• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with asList() method

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also Saurabh please Use Code Tags to make your program readable

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic