Forums Register Login

problem with asList() method

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
Also Saurabh please Use Code Tags to make your program readable

+Pie Number of slices to send: Send
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 ..

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 982 times.
Similar Threads
Callback Listener methods in EJB3.0
How do I add a row efficiently in a unidirectional one-to-many relationship?
Can we decrease the size of the ArrayList ?.
Help with writing a remove() method
how to Implement equals and hashcode method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:11:26.