aspose file tools*
The moose likes Beginning Java and the fly likes please help regarding collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please help regarding collections" Watch "please help regarding collections" New topic
Author

please help regarding collections

Rohit W. Tawde
Greenhorn

Joined: Mar 12, 2013
Posts: 25

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 ???


Rohit Tawde (SCJP5)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39386
    
  28
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

Joined: Mar 12, 2013
Posts: 25

Thank you so much guys.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Rohit W. Tawde wrote:Thank you so much guys.

You're most welcome.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39386
    
  28
Winston Gutkowski wrote: . . .
You're most welcome.

Winston
Agree
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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

Joined: Oct 13, 2005
Posts: 39386
    
  28
Yes, you are right. Copy of the reference to s, which is not changed when s changes.
Rohit W. Tawde
Greenhorn

Joined: Mar 12, 2013
Posts: 25

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help regarding collections