Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I Use Generics, But Not This Class<T> thing!

I am trying to call this method to concat two arrays using Google Collections

public static <T> T[] concat(T[] first,
                             T[] second,
                             Class<T> type)

It's returning empty results. I am using

ObjectArrays.concat(array1, array2, Blah.class)

which is the only thing that compiles.

array1 and array2 are of type Blah[].

What's the right syntax?

Bonus question: do other collections libraries have documentation with examples?

Edit: Problem was my bone-headed code.

public void register(ButtonPair[] pairs) {
    pairs = ObjectArrays.concat(this.pairs, pairs, ButtonPair.class);
}

the right side of the thing is okay, but the left side is not assigning to this.pairs due to the ambiguity. Sorry! And hats off to Google Collections!

like image 359
Dan Rosenstark Avatar asked Dec 02 '25 21:12

Dan Rosenstark


2 Answers

The following worked for me:


String[] arr1 = { "abc", "def" };
String[] arr2 = { "ghi", "jkl" };
String[] result = ObjectArrays.concat(arr1, arr2, String.class);

How are you getting the result from concat()?

like image 112
teto Avatar answered Dec 04 '25 10:12

teto


For some example usage of the Google Collections classes, check out the unit tests.

For example:

String[] result = ObjectArrays.concat(
    new String[] { "a", "b" }, new String[] { "c", "d" }, String.class);
assertEquals(String[].class, result.getClass());
assertContentsInOrder(Arrays.asList(result), "a", "b", "c", "d");

So, what the Class<T> notation means is that it needs you to specify what class the objects in the other two argument arrays belong to.

like image 21
pkaeding Avatar answered Dec 04 '25 09:12

pkaeding



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!