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?

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

x
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.

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
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 !!!
0
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:
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
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

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.
0
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.