Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Split String to ArrayList

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(...));
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But really you should try not to need an ArrayList. Program to the interface instead:
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys I'll give it a try and repost what solution(s) fixed it.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably instead of want
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic