Link to home
Start Free TrialLog in
Avatar of yarekGmail
yarekGmail

asked on

java array and list

hello experts:
this is the code I have

String blocked[] = blockedString.split(",");
List<String> bannedAsList = Arrays.asList(blocked); // this works !!!

But When I try to add a new object like:
bannedAsList.add("hello");

I got an exception !

1) why ?
2) How to add new objects to bannedAsList  ?

regards
Avatar of ksivananth
ksivananth
Flag of United States of America image

try this,

List<String> bannedAsList = new ArrayList<String>( Arrays.asList(blocked) ) ;
discard it, looks like asList itself returning ArrayList... what is the exception you get?
Avatar of Mick Barry
asList() returns a fixed size list
you need to create an ArrayList and copy the asList() into it (or add the elements from your array in a loop)

List bannedAsList = new ArrayList(Arrays.asList(blocked)); // this works !!!
ksivananth,

sorry buddy, your post wasn't there when i started
Avatar of aman123_123
aman123_123

Following is the javadoc of asList function :

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray. The returned list is serializable and implements RandomAccess.

This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
 
Parameters:
a the array by which the list will be backed
Returns:
a list view of the specified array


Lits returned is a fixed size and is a view of the specified array. Hence you are getting this exception.
>>sorry buddy, your post wasn't there when i started

no issues!
Avatar of yarekGmail

ASKER

NOT GOOD:
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
looks like you trying to cast the list returned from asList to ArrayList...

does the below works?
 List<String> bannedAsList = new ArrayList<String>( Arrays.asList(blocked) ) ;
NIO !
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
Should be as mentioned above.
Maybe this will help?

String blocked[] = blockedString.split(",");
List unmodifiableBannedAsList = Arrays.asList(blocked); // this works !!!
//List we get back here is internal to Arrays called Arrays.ArrayList, we can't
//modify it.
//Lets create a new list so that we can add more information to it.

List bannedList = new java.util.ArrayList();
bannedList.addAll(unmodifiableBannedAsList);
bannedAsList.add("hello");

Hope that helps.
Sorry if i've step on anyones toes.
ASKER CERTIFIED SOLUTION
Avatar of siddagrl
siddagrl
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Thought you wanted to "add" to your list. That code won't let you add to it.