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
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
discard it, looks like asList itself returning ArrayList... what is the exception you get?
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(bl ocked)); // this works !!!
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(bl
ksivananth,
sorry buddy, your post wasn't there when i started
sorry buddy, your post wasn't there when i started
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.
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!
no issues!
ASKER
NOT GOOD:
Exception in thread "main" java.lang.ClassCastExcepti on: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
Exception in thread "main" java.lang.ClassCastExcepti
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) ) ;
does the below works?
List<String> bannedAsList = new ArrayList<String>( Arrays.asList(blocked) ) ;
ASKER
NIO !
Exception in thread "main" java.lang.ClassCastExcepti on: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
Exception in thread "main" java.lang.ClassCastExcepti
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(unmodifi ableBanned AsList);
bannedAsList.add("hello");
Hope that helps.
Sorry if i've step on anyones toes.
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(unmodifi
bannedAsList.add("hello");
Hope that helps.
Sorry if i've step on anyones toes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thought you wanted to "add" to your list. That code won't let you add to it.
List<String> bannedAsList = new ArrayList<String>( Arrays.asList(blocked) ) ;