Как объединить два массива неопределенного типа в C # с помощью .NET 1.1 - PullRequest
2 голосов
/ 11 февраля 2010

Я пытаюсь создать служебный метод, который будет принимать два массива в качестве параметров, объединять их и возвращать полученный массив. Отказ от дубликатов.

В идеале параметры должны принимать любой тип массива, например int [] или string [].

Я использую C # .NET 1.1, поэтому у меня нет доступа к Generics или Array.Resize ().

Есть ли лучший способ объединить два массива, не зная их типа, но возвращая массив одного типа?

В это время следующий код возвращает массив объекта []. Я хотел бы, чтобы возвращаемый тип массива соответствовал параметрам.

public static object[] MergeArrays(object[] dest, object[] src)
{
    if (dest.GetType() != src.GetType())
        return null;    // The arrays are of different types

    if (dest.Equals(src))
        return dest;    // Don't merge with self

    // We now know there are two compatible and unique arrays
    int delta = src.Length;

    // Cycle through the passed materials and see if they already exist
    for (int i = 0; i < src.Length; i++)
        // Check to see if this material already exists
        for (int j = 0; j < dest.Length; j++)
            if (src[i] == dest[j])
            {
                // The material already exists, so we'll skip it
                src[i] = null;
                delta--;
                break;
            }

    // If there are still objects to merge, increase the array size
    if (delta > 0)
    {
        object[] = new object[dest.Length + delta];
        int index;

        // Copy the original array
        for (index = 0; index < dest.Length; index++)
            tmp[index] = dest[index];

        // Add the new elements
        for (int i = 0; i < src.Length; i++)
        {
            if (src[i] == null)
                continue;
            tmp[index++] = src[i];
        }
        dest = tmp;
    }
    return dest;
}

1 Ответ

1 голос
/ 11 февраля 2010

Я считаю, что это все законно .NET 1.1:

public static object[] Merge(object[] first, object[] second) {
    if (first == null) {
        throw new ArgumentNullException("first");
    }
    if (second == null) {
        throw new ArgumentNullException("second");
    }
    Type firstType = first.GetType();
    Type secondType = second.GetType();
    if (firstType != secondType) {
        throw new InvalidOperationException("type mismatch");
    }
    Hashtable table = new Hashtable();
    ArrayList items = new ArrayList();
    NewMethod(first, table, items);
    NewMethod(second, table, items);
    return (object[])items.ToArray(firstType.GetElementType());
}

static void NewMethod(object[] array, Hashtable table, ArrayList items) {
    for (int i = 0; i < array.Length; i++) {
        object item = array[i];
        if (!table.Contains(item)) {
            table.Add(item, 1);
            items.Add(item);
        }
    }
}

Мне лень найти подходящее имя для NewMethod, поэтому я просто позволю Visual Studio присвоить ему имя по умолчанию после процедуры извлечения в метод.

Использование:

object[] a = new string[10];
object[] b = new string[10];

for(int i = 0; i < 10; i++) {
    a[i] = i.ToString();
    b[i] = (i + 5).ToString();
}

object[] c = Merge(a, b);
Console.WriteLine(c.Length);
Console.WriteLine(c.GetType());
for (int i = 0; i < c.Length; i++) {
    Console.WriteLine(c[i]);
}

Выход:

15
System.String[]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15   

Обратите внимание, что если вы хотите поместить массив из двух T[], где T : ValueType в Merge, вы должны сначала упаковать элементы и ввести массив как object[]; это потому, что нет преобразования T[] в object[] при T : ValueType. Более того, поскольку GetType не является виртуальным, лучшее, что вы можете получить в этом случае, это object[], а не T[].

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...