• 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

copying a List

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this case of referencing a list:

In the end I want vertexIndices to contain the numbers passed into the constructor. I don't know whether I need a shallow or non-shallow (how's non-shallow named?) copy.
From what I've found out there are 3 ways of copying Indices but I don't know which one is appropriate, since it comes to shallow copying:

1. This ensures a new independent List (?):

2. This does create a shallow copy:

3. I can't tell what this does:

but I guess it also shallow-copies vI into vertexIndices; then why not use this instead of (2)?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This:just copies the reference. That means that the vertexIndices member variable is now pointing at exactly the same list as the vI variable. This is not usually what you want because it breaks encapsulation. If the calling code keeps a reference to the list passed in then it can now manipulate that list, which means it can change the state of the Face object without calling any methods on it.

1 & 2 are effectively the same if you pass in an ArrayList - they'll give you a new List. But any other type of list will cause a ClassCastException, which isn't good. For that, and because I'm not a big fan of the clone() method in general I'd far prefer 1.

If you want a deep (i.e. non-shallow) copy, then not only do you need to copy the List, you also need to copy every value in it. In this case you don't need that because they contain Integer objects, which are immutable, and so never need copying. If you had a list of mutable objects you might need to do that, though.

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A shallow copy of the List<Integer> passed to the constructor will do, because Integer objects are immutable.
The most convenient way to do that is to use the ArrayList constructor that accepts a Collection, as you have in the first example, which will create a new instance of ArrayList that references the same Integer objects in the same order. The second solution that uses the clone() implementation of ArrayList will accomplish the same thing, but the catch is you have to be sure it's an ArrayList before you cast it as such. The third solution, however, will not. It will simply cause vertexIndices to reference the same List instance which could be manipulated directly using the reference passed to the Face class' constructor, which is something you'll definitely want to avoid, and is infact the reason you'd want to perform a shallow copy. By the way, the counterpart to a shallow copy is a deep copy, which in this example wouldn't just create a new ArrayList that references the same Integer objects, but it would create new and equivalent Integer objects as well.

Edit: ack, too late.
 
Andrei Marian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both. Not only I have an answer to my question but I learned new things as well.
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic