• 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

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: 43081
77
  • 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 !!!
 
Do Re Mi Fa So La Tiny Ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic