2

I'm trying to merge 2 int arrays using this custom function I found on Google:

public static <T> T[] arrayMerge(T[]... arrays)
{
    int count = 0;
    for (T[] array : arrays) count += array.length;

    T[] mergedArray = (T[]) Array.newInstance(arrays[0][0].getClass(),count);
    int start = 0;
    for (T[] array : arrays) {
        System.arraycopy(array, 0, mergedArray, start, array.length);
        start += array.length;
    }
    return (T[]) mergedArray;
}

but I'm fail to understand what parameters this function takes. I was hoping it would work like arrayMerge(int[], int[]), but Eclipse tells me it doesn't take these arguments. I can't Google a capital T to find an answer.

You can answer in a form or reading material, but an example of using this function to merge 2 int arrays would be nice (does it eliminate duplicates, if not, how can I also achieve that?).

user781655
  • 415
  • 1
  • 6
  • 9
  • 1
    You can avoid the rather ugly (and dangerous) `arrays[0][0]` by using `Arrays.copyOf()` as shown in [a previous answer from me](http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java/784842#784842). The problem with `arrays[0][0]` is two-fold: the first array *might* be empty, in which case you'd get a `ArrayIndexOutOfBoundsException` and the first element might not be of the type of the array (e.g. it could be an `Integer` in an `Object[]`) or it could even be `null`. – Joachim Sauer Aug 10 '11 at 06:56

3 Answers3

1

The method takes any number of arrays, all having the same type:

String[] array1 = ... ;
String[] array2 = ... ;
String[] array3 = ... ;
String[] mergedArray = ArrayHelper.arrayMerge(array1, array2, array3);

However, due the way generics work in Java, you cannot pass an array of a primitive type (such as int[]).

Codo
  • 75,595
  • 17
  • 168
  • 206
  • Then how come I'm getting: The method arrayMerge(T[]...) in the type Main is not applicable for the arguments (int[], int[]) – user781655 Aug 10 '11 at 06:54
  • Because generics do not work with primitive types. Even when used as array elements. T can only stand in for types, which can be assigned to Object without conversions (like boxing) – Dirk Aug 10 '11 at 07:08
  • @Dirk: those are usually called "reference types". – Joachim Sauer Aug 10 '11 at 07:10
  • @joachim - I tried to avoid the lingo (and utterly failed - “generics“, “boxing“), but thanks anyway :-) – Dirk Aug 10 '11 at 07:17
  • @Dirk: I thought so, actually my comment was directed at user781655 as well. Just wanted to get the term out there. – Joachim Sauer Aug 10 '11 at 07:18
  • @user781655 - If you want to use this function to merge 2 (or more) integer arrays, you can use the wrapper class `Integer` instead of the primitive type `int`. – phuibers Aug 10 '11 at 07:45
0

The code you have post here is for any type of array not only int and for more than 2 arrays to merge. (unlimited)
T stand for a generic Type. see Generics in Java.

You can use only the System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

  • src the source array.
  • srcPos starting position in the source array.
  • dest the destination array.
  • destPos starting position in the destination data.
  • length the number of array elements to be copied.

First make an array which has the length of both arrays.

public merge(int[] x, int[] y) {

    int[] merged = new int[x.length + y.length]

Than copy with System.arraycopy the first array x into merged at 0.
And copy with System.arraycopy the 2nd array y into merged at x.length

But it doesn't eliminate duplicates.

For no duplicates you can use a Set. But we have to work with Integer not int.
So we have to convert to Integer an back to int. like:

HashSet<Integer> set = new HashSet<Integer>();
for( int i : x ) {
    set.add( Integer.valueOf( i ) );
}
for( int i : y ) {
    set.add( Integer.valueOf( i ) );
}
int[] merged = new int[set.size()];
int i = 0;
for( Integer value : set ) {
    merged[i++] = value.intValue();
}
oliholz
  • 7,447
  • 2
  • 43
  • 82
-1

That syntax is called varargs - it means it will accept any number of parameters of that type. Removing the generics to simply the explanation, a method defined as this:

public static void someMethod(int... numbers) {
    // some code
}

can be called like this:

someMethod(5); // The "numbers" parameter will be an int array size 1: [5]
someMethod(1,2,3); // The "numbers" parameter will be an int array size 3: [1, 2, 3]
someMethod(); // The "numbers" parameter will be an int array size 0: []

It's like a shorthand for this:

someMethod(new int[] {1, 2, 3});

but less ugly.

In your example, the array is a typed generic array - you don't know what the element type is until it's called.

Bohemian
  • 412,405
  • 93
  • 575
  • 722