This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please help regarding collections

 
Rohit W. Tawde
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added a string object in an ArrayList.
Then i made the string reference point to another string object.
And after that when i iterate through the ArrayList, it still displays the old string object.
I am confused as to how this happens.
Could you kindly explain.

Below is the code

String s = new String("Stevens");

ArrayList<String> list = new ArrayList<String>();

name.add("Aditya");
name.add("Akash");
name.add(2,s);
name.add("Smith");
name.add("Darren");

s = new String("Ronan");

for (String str : list) {
System.out.println(str);
}

Below is the output,

Aditya
Akash
Stevens
Smith
Darren

Should not the list display Ronan , instead of Stevens ???
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit W. Tawde wrote:I am confused as to how this happens.

It happens because Strings are immutable (Google it) - that is to say: you can't change a String, you can only reassign it with a new different one.

So, while 's' may point to whatever new value you've given it, the element retrieved by name.get() will still point to the old one.

You CAN, however, change an element's value. Have a look at the ArrayList.set() method.

HIH

Winston
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should by now know how to use code tags.

Why do you think that re‑assigning a reference outside the List would change the contents of the List? You do not add the reference s to the List, you add the String "Stevens". By changing the reference s, you do not change the reference inside the List.

And don’t use new String("Ronan"). Use "Ronan".
 
Rohit W. Tawde
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much guys.
 
Winston Gutkowski
Bartender
Pie
Posts: 10109
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit W. Tawde wrote:Thank you so much guys.

You're most welcome.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
You're most welcome.

Winston
Agree
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You do not add the reference s to the List, you add the String "Stevens". By changing the reference s, you do not change the reference inside the List.

Surely, as your second sentence suggests, you mean that you add a copy of the reference s to the List. Strings, or any other objects for that metter, are never added to Lists.


 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right. Copy of the reference to s, which is not changed when s changes.
 
Rohit W. Tawde
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you add the copy of the reference s to the list, say 'p'.
So if we change the object to which the reference s is pointing, it has no effect on the reference added to the list ie 'p'.
And the only way we could change the object in the list is by name.add(2,"Ronan") OR name.set(2,"Ronan").
I hope i am right.
Kindly confirm it. Please.
Thank you.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohit W. Tawde wrote:So you add the copy of the reference s to the list, say 'p'.

Correct. At least if I understand what you're trying to say.

s is a reference variable. Its value is copied and passed to the add method, which in turn copies it to place it into its list structure, so let's call the list's copy 'p'. So now the list has a reference (p) with the same value as the s variable. Both s and the p point to the same object.

So if we change the object to which the reference s is pointing, it has no effect on the reference added to the list ie 'p'.

Correct. If we do s = something_else; that just puts a different value into s. That is, s now points to a different object, or has the value null, so it doesn't point to any object. That has no effect whatsoever on the list's 'p' reference, or on the object that s previously pointed to.

And the only way we could change the object in the list is by name.add(2,"Ronan") OR name.set(2,"Ronan").

Depends what you mean by "change the object." The phrase is a bit vague. However, if you mean "The only say to change which object that list element points to..." then, yes, adding at that position (or before that position, so it has to shift), or setting at that position, or calling remove() on that position or a preceding one, or clearing the list.

In short, the only way to affect which object a given element of the list refers to is through the list's methods.

Note, however, that if we our original value in our s variable, and then we do something like s.someField = something or s.setSomeField(something), since it's the same object that the list is pointing to, the list will see that change.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic