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:

I got an exception !

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
Mick BarryJava DeveloperCommented:
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 !!!
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Mick BarryJava DeveloperCommented:

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");
a the array by which the list will be backed
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!
yarekGmailAuthor Commented:
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) ) ;
yarekGmailAuthor Commented:
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();

Hope that helps.
Sorry if i've step on anyones toes.

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):

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);
        l.set(1, "c");

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mick BarryJava DeveloperCommented:

Thought you wanted to "add" to your list. That code won't let you add to it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.