File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with asList() method

 
Saurabh Naik
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can solve your problem a couple of different ways:

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

Or you could do:

myCollection.addAll(Arrays.asList(myArray));

Then, use the Collection.toArray(T[]) method, where T is the type of the array elements.
 
Saurabh Naik
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.addAll(Arrays.asList(a));

al.remove(new Integer(78));

a = al.toArray(a);

for(int i: a)
System.out.println(i);


}

}

but finally when I am printing array its throwing nullpointerException
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also Saurabh please Use Code Tags to make your program readable

 
Albert Kam
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 381
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic