1

I have the following code snippet, which I can't understand why doesn't work:

List<Object[]> listOfObjectArrays;
listOfObjectArrays = new ArrayList<>();
Object[] objectArray = new Object[] {1, "two", null};
listOfObjectArrays.add(objectArray);
// works just fine

listOfObjectArrays = Arrays.asList(objectArray, objectArray);
// works just fine

listOfObjectArrays = Arrays.asList(objectArray); // *
// compile error: Incompatible types. Required: List<java.lang.Object[]> Found: List<java.lang.Object>

listOfObjectArrays = Arrays.asList(new Object[] {1, "two", null});
// compile error: Incompatible types. Required: List<java.lang.Object[]> Found: List<java.lang.Object>

Could somebody please point me in the right direction?

I already saw Jon Skeet's answer on an other question, but the last example there does not work for me. Even if I add a cast to either Object or Object[] in the line marked with * I get a compile error.

Community
  • 1
  • 1
zovits
  • 906
  • 16
  • 27
  • 1
    `Arrays.asList(new Object[] {yourArray});`. `asList` uess a varargs argument, and so the array gets expanded as multiple arguments. – eric.m Sep 11 '15 at 11:23
  • Or `Arrays>.asList(objectArray)` – Jorge Campos Sep 11 '15 at 11:24
  • 1
    Your listOfObjectArrays contains Arrays.asList converts an array of object[] to List You need an object array that's like Object[][] – kevintjuh93 Sep 11 '15 at 11:26
  • Thank you for the quick replies, Emd4600: It is indeed a convincing explanation, thank you! Jorge: Your example doesn't seem to work, are you sure this is correct? Kevin: Thank you, I've tried it as embedded arrays ( `Arrays.asList(new Object[][] {{1}, {"two"}, {null}});` ) and it works just fine. The error message is a bit misleading though, but reasonable if the array is expanded. – zovits Sep 11 '15 at 11:32
  • 1
    Please read this to understand why your array of Objects is treated as variable argument list of Objects: http://stackoverflow.com/questions/2925153/can-i-pass-an-array-as-arguments-to-a-method-with-variable-arguments-in-java – midor Sep 11 '15 at 11:33

3 Answers3

2

You can always tell Java that you want a list of Object[] by specifying the type parameter explicitly:

Object[] objectArray = { 1, "two", null };
List<Object[]> listOfObjectArrays = Arrays.<Object[]>asList(objectArray);
Keppil
  • 45,603
  • 8
  • 97
  • 119
1
List<Object[]> listOfObjectArrays;
listOfObjectArrays = new ArrayList<>();
Object[][] objectArray = new Object[][] {{1, "two", null}};
listOfObjectArrays.add(objectArray[0]);
// works just fine

listOfObjectArrays = Arrays.asList(objectArray);
// works just fine

You need to do this. Your list contains Object[], when you do Arrays.asList it basically just iterates over the array and adds every index to the list. Like:

List<T> list = new List<>();
for (Object obj : objectArray) {
     list.add(obj);
}

After this it returns the list. As you can see it will return List, but you require List

kevintjuh93
  • 1,010
  • 7
  • 22
0

Keppil nicely formulated a line that would solve my current problem, but Kevin's answer pointed me in the right direction to understand the situation.

Until now I used the abstraction that Arrays.asList() wraps the argument(s) in an array regardless of content and then uses it to create an ArrayList. According to this model my call should have resulted in an ArrayList backed by an array containing the passed Object[] parameter.

Looking at the Arrays.asList() source it became clear that my call (listOfObjectArrays = Arrays.asList(objectArray);) would have resulted in a List backed by objectArray itself, creating an ArrayList<Object>. So in order to have a list of Object[] arrays, I'd need to pass an array of Object[] arrays (as Kevin suggested), or explicitly set the type parameter of Arrays to Object[], forcing Arrays.asList() to interpret the argument as an array consisting of Object[] items (as per Keppil).

Community
  • 1
  • 1
zovits
  • 906
  • 16
  • 27