• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings in a List  RSS feed

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain why the "s haven't gone away when I try to get rid of them in the List?



Output currently is:
"Hi"
Hi
"Yo"
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because in your enhanced for loop, the variable n is a local variable which is reassigned to a new String returned from n.replace("\"", "") and doesn't affect the value stored in the List. The loop is functionally equivalent toYou can easily see that this won't change the value of the String at names.get(i).
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote:

On a side note, Java 5.0 added varargs and that's enabled for Arrays.asList too. So you can also write the following:
In the background the exact same thing happens, but this is easier to read and write.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I guess I need to change the 2nd part to
Is there a way to do this while retaining the enhanced for loop?
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would probably be inclined to get the iterator for your list and use that.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String is immutable, and a List contains references to its contents. Any amount of fudging around won't change the String referenced from the List.

The only valid approach will be to remove the string form the list, and add the parsed string in its place (or at the end if order isn't an issue).
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernie Mcracken wrote:I would probably be inclined to get the iterator for your list and use that.


I haven't used iterators before but I think you just run into the same problem as with enhanced for:



So I guess enhanced-for is out and we just have to step through using a standard for loop.
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah sorry I meant that to say use ListIterator,



Output is

Yo
Hi
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote:So I guess enhanced-for is out and we just have to step through using a standard for loop.

Unless you want to create a new List:
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I didn't know about the ListIterator's set method.

Using for:

which is equivalent to

although I suppose a while loop is more readable.

Matthew - yes, that's another good idea, thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!