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
yarekGmailAsked:
Who is Participating?
 
siddagrlCommented:
@AskAnd

FYI,
The list returned is not "internal" to Arrays.
We can not add / delete elements since it is fixed-size.

Also point to note here is that if you make changes (yes, we can modify!) in the list, that gets reflected to underlying array.

As per javadoc (1.5):
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html

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

e.g., output of attached code is as follows:
[a, b, c]
[a, c, c]
import java.util.*;

public class a {
    public static void main(String[] args)
    {
        String s[] = {"a", "b", "c"};
        List<String> l = Arrays.asList(s);
        System.out.println(Arrays.toString(s));
        l.set(1, "c");
        System.out.println(Arrays.toString(s));
    }
}

Open in new window

0
 
ksivananthCommented:
try this,

List<String> bannedAsList = new ArrayList<String>( Arrays.asList(blocked) ) ;
0
 
ksivananthCommented:
discard it, looks like asList itself returning ArrayList... what is the exception you get?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
objectsCommented:
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 !!!
0
 
objectsCommented:
ksivananth,

sorry buddy, your post wasn't there when i started
0
 
aman123_123Commented:
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.
0
 
ksivananthCommented:
>>sorry buddy, your post wasn't there when i started

no issues!
0
 
yarekGmailAuthor Commented:
NOT GOOD:
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
0
 
ksivananthCommented:
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) ) ;
0
 
yarekGmailAuthor Commented:
NIO !
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
0
 
AskAndCommented:
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.
0
 
objectsCommented:

Thought you wanted to "add" to your list. That code won't let you add to it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.