• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings in a List

 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah sorry I meant that to say use ListIterator,



Output is

Yo
Hi
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic