When I try to create an ArrayList myArrayList from an array, using Arrays.asList(myArray), I am not getting the List of elements in myArray. Instead I get list of Array.

The size of myArrayList is 1 . When I try to do myArrayList.toArray(), I am getting a two dimensional array. What to do to get the elements of myArray in a list? Is iterating the only option??

share|improve this question

60% accept rate
Looks like your myArray is a two dimensional array. Have you made sure there are no double braces like {{"one", "two"}}? – adarshr Sep 22 '11 at 11:50
1  
@bluish Sorry, I wasn't doing that. I will correct myself. Thanks. @ others Just as Banther said, I was trying it on an int[]. Next time onwards, I will try to post the code snippet. Thanks – Suma Sep 22 '11 at 12:16
feedback

5 Answers

up vote 2 down vote accepted

What is the type of myArray? You cannot use Arrays.asList with an array of primitive type (such as int[]). You need to use loop in that case.

share|improve this answer
Thanks Banther. I was trying it for an int[] only. – Suma Sep 22 '11 at 12:15
@Suma this is why posting code in a question is a good idea. It would have reduced a lot of guesswork. – John B Sep 22 '11 at 12:23
@JohnB yes, I understand now. Sorry:( – Suma Sep 22 '11 at 12:25
feedback

Firstly, the asList method is the right method:

Integer[] myArray = new Integer[3];
List<Integer> myArrayList = Arrays.asList(myArray);
System.out.println(myArrayList.size()); // prints 3, as expected

The problem may be that you are calling the varargs asList method in such a way that java is interpreting your parameter as the first varargs value (and not as an array of values).

Object myArray = new Integer[3];
List<Object> myArrayList = Arrays.asList(myArray);
System.out.println(myArrayList.size()); // prints 1 - java invoked it as an array of Integer[]

To fix this problem, try casting your parameter as Object[] to force the varargs invocation, eg:

Object myArray = new Integer[3];
List<Object> myArrayList = Arrays.asList((Object[]) myArray); // Note cast here
System.out.println(myArrayList.size()); // prints 3, as desired
share|improve this answer
feedback

Would this work?

Object[] myArray = new Object[4]; //change this to whatever object you have
ArrayList<Object> list = new ArrayList<Object>();
for (Object thing : myArray) list.add(thing);
share|improve this answer
feedback

Try providing the generic type in the method call. The following gives me a list of 2 String elements.

    String[] strings = new String[]{"1", "2"};
    List<String> list = Arrays.<String>asList(strings);
    System.out.println(list.size());
share|improve this answer
feedback

There are different ways by which you can achieve it.3 example of converting array to arraylist and arraylist to array in java might help.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.