File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Split String to ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Split String to ArrayList" Watch "Split String to ArrayList" New topic
Author

Split String to ArrayList

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
I'm having trouble trying to split a string into an ArrayList. Here's what I have :

If I wrap this in a try-catch statement the execution goes to the else clause and aborts the loop. The parameter in Eclipse for Level1 is set to "Str1,Str2,Str3,Str4,Str5".
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Arrays.asList() doesn't return an ArrayList. It returns an implementation of the List interface. If you really need an ArrayList you can try: new ArrayList<String>(Arrays.asList(...));


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18132
    
    8

But really you should try not to need an ArrayList. Program to the interface instead:
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
I was using ArrayList because of the flexibility to add and remove items from it later on, does List allow the same sort of flexibility?
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
Is there a way to "cast" a List into an ArrayList with using it in a constructor? I attempted to cast it to see what happened and Eclipse didn't complain about it but at runtime it bailed.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18132
    
    8

Rob Hunter wrote:I was using ArrayList because of the flexibility to add and remove items from it later on, does List allow the same sort of flexibility?


That's a meaningless question. A List variable can refer to all sorts of objects which implement the List interface. Some classes which implement List allow you to modify the list that way, others don't.

So the question you should ask is, does the result of Arrays.asList() return a list which you can add entries to? And the answer is...
The API documentation wrote:Returns a fixed-size list backed by the specified array.

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
This works fine. If I wanted to build an array of varying sized Lists whats the easiest way to accomplish that?



What I've tried without success. When running the app doesn't like the clear() method line.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18132
    
    8

Well, no, you can't "clear" a fixed-size list. Changing the size of the list to zero can't be done.

But I don't understand why you would want to create a list, with data in it, and then immediately clear the list. What's the point?
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
Here's an example of what I'm trying to do, either there's a better way of doing it or it'll be easier to understand what I'm trying to accomplish. I have multiple strings (comma-delimited) that I need to split and store into an "array". Each of these strings will be varying sizes. So I'll be looping through parameters coming in, in the form of "Level"+counter. My concern (whereby I want to clear) is if an array is 5 elements and next time through the array is 4 long that the 5th element will stick around. Is there a need to do that then? I'm fairly new to Java so just getting my feet wet. Any help would be appreciated and thanks for the responses so far. Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18132
    
    8

I'm not sure I understand all of that.

Are you suggesting that if you create an array with 5 elements, and then later you create another array with 4 elements, then the latter array will actually have 5 elements? It's easy enough to write a program to test that hypothesis:
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
More like if I use a List within a loop. Let's say the first time in listOfElements gets set to 5 items and the second time through the string when split is actually 4 items. Will the 5th item remain in the List? If the first 5 elements were A,B,C,D, and E and then in the next loop the elements were 1,2,3, and 4 would the array the second time through be 1, 2, 3, 4, and E? I've seen it before in another language and thought I'd ask. Thanks.



Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
Disregard I don't need a clear, the size is reset which is great news. I recall having to reset the variable because of the situation I was asking about above. Thanks for the help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

I'll quote Wouter just because his answer is spot-on:
Wouter Oet wrote:Arrays.asList() doesn't return an ArrayList. It returns an implementation of the List interface. If you really need an ArrayList you can try: new ArrayList<String>(Arrays.asList(...));

By creating a new ArrayList with the return value of Arrays.asList as argument, the ArrayList will contain all the array elements - they are copied into the ArrayList. and because you're explicitly using an ArrayList you can modify it however you like.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
Disregard I don't need a clear, the size is reset which is great news. I recall having to reset the variable because of the situation I was asking about above. Thanks for the help.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
The String.split method creates a new array each time it is called, and the Array.asList method creates a new List each time it is called.
Of course you could have found this out by creating a quick test program.

Edit - boy I'm slow tonight


Joanne
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
Okay the question that will get someone a free box of imaginary cookies: how can I keep a list of lists? I tried :



... the dataList line went immediately (after stopping in the store to get the cookies) to the else clause and the Exception's getMessage returned null.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Rob Hunter wrote:went immediately ... to the else clause and the Exception's getMessage returned null.
I assume you meant the exception clause.

What's the value of levelCount. It must be >= 0 and <= the current size of the list. The fact that you are decrementing it each time round the loop suggest that it is greater than the current size of the list and so the add() method will throw an IndexOutOfBoundsException.
If you want the first item you add to appear at the end of the list, then just add each item at position zero. This will push all the items in the list down by one place each time.

Edit: See Sean's post below before trying to fix the problems I have pointed out.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Rob Hunter wrote:... the dataList line went immediately (after stopping in the store to get the cookies) to the else clause and the Exception's getMessage returned null.

It is null because you have recieved a NullPointerException, you should printStackTrace() rather than getting the message.

The reason is that you have not initialised the list:



Sean


I love this place!
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
Thanks guys I'll give it a try and repost what solution(s) fixed it.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You probably instead of want
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Split String to ArrayList
 
Similar Threads
Switch statement
Polymorphism on data members
splitting arrays
Polymorphism on data members
testing a delimeter