views:

259

answers:

4

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!

+3  A: 

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.

pkaeding
cool, then it must be me. I'll recheck my code
Yar
+3  A: 

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()?

teto
Yes, I am getting the result of the concat, but I must be messing something else up. I'll check my code. Thanks for that.
Yar
Marking this as best answer, because in fact I was, in a sense, not catching the return result. But I thought I was :) See the question
Yar
+1  A: 

Your syntax looks totally correct. I think the problem must be elsewhere. Are you 100% certain about the input values? Here is a test case:

import com.google.common.collect.ObjectArrays;

public class ObjectArrayTest
{
    public static void main(String[] args)
    {
        String[] first = new String[] { "Fire", "Earth" };
        String[] second = new String[] { "Water", "Air" };

        String[] result = ObjectArrays.concat(first, second, String.class);

        for (String s : result)
        {
            System.out.println (s);
        }
    }
}
amarillion
Cool, thanks, I see it must be elsewhere in my code. I thought I had bracketed the problem but I did not. Okay, last +1 for this answer :)
Yar
A: 

Isn't this because you are not assigning the result to the instance variable but to the method variable.

That is this:

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

should be

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

Incidentally, this is why at our shop we have have a different naming convention for method parameters and variables than that for instance variables (though not the awful prefixing/suffixing of instance variables like _someInstanceVar).

Software Monkey
Yes, as I said in the question, that is the problem. What's the naming convention you use?
Yar
We use full words for instance/class variables and 3 letter abbreviations for method parms and variables - it works really well for the most part, though occasionally a 3 letter word is what you want for the parm.
Software Monkey