• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Collections - Arrays.asList() / List.add()

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below code compiles fine, however I am getting runtime exception
at Line 12...

Iam trying to insert an element of type Integer into a List<Integer>

Why is an "UnsupportedOperationException" being generated...

/********************CODE********************************************/
import java.util.*;

public class TestAsList{

public static void main(String[] args){

Integer[] arr =new Integer[] {12,34,56,78,98,23};


List<Integer> aList = Arrays.asList(arr);

aList.add(new Integer(1232)); //Line 12

System.out.println("Printing aList :" + aList);

}

}

//*********************output***************************************/

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)
at TestAsList.main(TestAsList.java:12)
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java docs for Arrays.asList say that "Returns a fixed-size list backed by the specified array." If you want a modifiable list you can do something like "ArrayList anotherList = new ArrayList(aList)".
 
Masood Alam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks it works...

However I do still have the same doubt, as per the documentation

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

Does this mean we cannot change the list..then how will it change the array as per the line "(Changes to the returned list "write through" to the array.)"
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Masood Alam:
Does this mean we cannot change the list

You cannot change the size of the list. But you can do the following:
 
Masood Alam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you !!!
reply
    Bookmark Topic Watch Topic
  • New Topic