my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with asList() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with asList() method" Watch "problem with asList() method" New topic

problem with asList() method

Saurabh Naik
Ranch Hand

Joined: Apr 24, 2008
Posts: 87
I used Arrays.asList() method it returns List.

so whenever I perform some operation on List i.e add or remove its throwing unsupportedException..

my problem is - how can you add or delete elements in an array.. whenever you add or remove any element its size should get automatically affected

so my solution is to convert array to some collection perform operation there and again covert that collection in to an array.

so is this correct solution for my problem ?

if yes then how to avoid that unsupported Exception.

SCJP 6 (88%), SCWCD 5 (78%), preparing for SCBCD
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You can solve your problem a couple of different ways:

Collection myCollection = someConcreteCollection();
Collections.addAll(c, myArray)

Or you could do:


Then, use the Collection.toArray(T[]) method, where T is the type of the array elements.

All code in my posts, unless a source is explicitly mentioned, is my own.
Saurabh Naik
Ranch Hand

Joined: Apr 24, 2008
Posts: 87
import java.util.*;

public class ConversionTo

public static void main(String args[])
Integer a[] = {23,56,89,78,45,12};

ArrayList<Integer> al = new ArrayList<Integer>();


al.remove(new Integer(78));

a = al.toArray(a);

for(int i: a)



but finally when I am printing array its throwing nullpointerException
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

Saurabh, this is from the documentation of the asList(T[] a) method

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

So since your list fits into the array, the last spot is set to null and while iterating it, the compiler tries to unbox it to int (from Integer) and thus you get the exception...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

Also Saurabh please Use Code Tags to make your program readable

Albert Kam
Ranch Hand

Joined: Oct 18, 2007
Posts: 58
Hello ..

I didnt understand why such error could happen ..
I knew that the array returned could grow larger in size if the supplied array doesnt have enough room,
but this is really another story.

Btw it's the api doc for the List's public Object[] toArray(Object[] a), not asList()

Thanks for the explanation ..

Be nice !
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

Albert Kam wrote:Btw it's the api doc for the List's public Object[] toArray(Object[] a), not asList()

I'm a fool. Don't mind such mistakes of mine
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
The usability of the asList() shall be taken in when you wants to play around the arrays except modifying it.
Since arrays can not be scaled or diminised at the runtime so is the list created on the top of it has no provistion for doing the same.
Same can be derived after seeing the JDK source code as well.

So there is no problem as such with asList().There are many alternative ways to sort out your porblem as discussed above.

~Sanjeev Singh<br />SCJP 1.5
It is sorta covered in the JavaRanch Style Guide.
subject: problem with asList() method
Similar Threads
How do I add a row efficiently in a unidirectional one-to-many relationship?
Help with writing a remove() method
Can we decrease the size of the ArrayList ?.
Callback Listener methods in EJB3.0
how to Implement equals and hashcode method