• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections.reverse gets weird results  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 257
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's the best title I could think of anyway,I am pretty confused here I created an ArrayList from the Ball class I made,and when I use Collections.reverse on it I get weird results and I'm not sure why,

anyway firstly here is my code



it prints A,B,C,D,E for list 2 and  E,D,C,B,A for list

but when I copy the list this way



when I print both lists they both get reversed?
 
Norm Radder
Rancher
Posts: 1873
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I copy the list this way


list2 = list

That does not copy the list.  It copies the reference to the list.  After that assignment statement there are two variables that refer to the same list.  There is only one list with two references to it.
 
Adam Chalkley
Ranch Hand
Posts: 257
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are they both shallow copys?



why when I do this list gets printed in it's reverse order,and when I print list2 it gets printed in it's normal order?

where as if I uncomment the other two lines and comment out one both lists seem to effected by Collections.reverse?



both lists will now be printed in reverse order
 
Norm Radder
Rancher
Posts: 1873
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are they both shallow copys?

No, this is not a copy of the list.  It copies the reference to the list.

There is still only one List after that statement.
 
Adam Chalkley
Ranch Hand
Posts: 257
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Norm,

I understand that but is



a deep copy or shallow copy?
 
Norm Radder
Rancher
Posts: 1873
26
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a deep copy or shallow copy?

Have you tried changing elements in one of the lists and print them to see if the change is in one or both?
Next use a class that you can change the contents of and try changing the contents in one list and see what happens.
A simple class with a public variable that can be set and a toString method to show when the list is printed would do.
 
Stephan van Hulst
Saloon Keeper
Posts: 7376
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless the documentation explicitly states that it's a deep copy, you should always asume a shallow copy. If your Ball class is mutable, changing a ball in one list will affect the corresponding ball in the other list.
 
Adam Chalkley
Ranch Hand
Posts: 257
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
a deep copy or shallow copy?

Have you tried changing elements in one of the lists and print them to see if the change is in one or both?
Next use a class that you can change the contents of and try changing the contents in one list and see what happens.
A simple class with a public variable that can be set and a toString method to show when the list is printed would do.


thanks for the replies Stephan and Norm

when I copy with ArrayList<Ball> list2 = new ArrayList<Ball><list>;

when I remove an element from list2 or list it only affects one list and not the other,

I'm not sure what you mean by the second part
 
Norm Radder
Rancher
Posts: 1873
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you mean by the second part

Are you asking about how to determine if a deep copy is made?
To detect a deep copy you need a mutable class as explained by Stephan. Then make a copy of the list, change the contents of an item in one list and see if the change is visible in the second list.

Here is some code that copies arrays and demonstrates different types of copies:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!