Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

copying arrays from one arraylist to another arraylist

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good day,

This is my code,

int [] test = {1000,2000,3000,4000,5000};
int [] test2 = {8000,4000,3000,2000,1000};

ArrayList<int []> editable = new ArrayList<int []>();
ArrayList<int []> backUp = new ArrayList<int []>();


editable.add(test);
backUp.addAll(editable);
editable.add(test2);

System.out.println("size of editable "+editable.size());
System.out.println("size of backUp "+backUp .size());
System.out.println("before, in backUp "+backUp .get(0)[0]);
editable.get(0)[0]=5;
System.out.println("after, in editable "+editable.get(0)[0]);
System.out.println("after, in backUp "+backUp .get(0)[0]);

o/p -

size of editable 2
size of backUp 1
before, in backUp 1000
after, in editable 5
after, in backUp 5


i have a group of arrays in an ArrayList(let's call it an editable arraylist ). i start manipulating the collection. if something goes wrong in manipulation, i should go back to my original collection. to achieve this, i, use another arraylist(let's call this as an arraylist that i should go back to if something goes wrong in manipulation; simply a back-up arraylist ) that has the same set of arrays. the problem is when i edit any of these arrays in editable arraylist, that edit gets reflected in the back-up arrayList.

ArrayList.addAll() is not helping me . so i changed my code to,

int [] test = {1000,2000,3000,4000,5000};
int [] copyTest = new int[test.length];
int [] test2 = {8000,4000,3000,2000,1000};

ArrayList<int []> editable = new ArrayList<int []>();
ArrayList<int []> backUp = new ArrayList<int []>();

editable.add(test);
editable.add(test2);

System.arraycopy(test, 0, copyTest, 0, test.length);
backUp.add(copyTest);

System.out.println("size of editable "+editable.size());
System.out.println("size of backUp "+backUp.size());
System.out.println("before, in backUp "+backUp.get(0)[0]);
editable.get(0)[0]=5;
System.out.println("after, in editable "+editable.get(0)[0]);
System.out.println("after, in backUp "+backUp.get(0)[0]);

o/p -

size of editable 2
size of editable 1
before, in backUp 1000
after, in editable 5
after, in backUp 1000


i copied the array in editable arrayList in to another array, and added the new array in the back-up arrayList. now changes made in editable arrayList are not reflected in back-up arrayList.

Note: addAll() too, uses arraycopy. but i think, it's not copying the individual arrays but the whole collection, which of course, is converted to an array using toArray(). now it's an array of arrays case, where the outer array is copied rather than the inner arrays. am i right?

my question is : Is there a way to do this more effectively. say if i have an arrayList with 1000 arrays, how to copy all those arrays in to another arrayList, so that whatever changes made in one arrayList(editable) is not reflected in the other(backUp).

have a great day,
krishna prasad g
 
Marshal
Posts: 74387
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags; they make you post much easier to read.

The whole idea of a Collection is to store objects, not to make them unmodifiable. If you want to store a copy of test2 so it can't be modified, maybe this will work:

backUp.add(test2.clone());
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankyou campbell,
sorry for the inconvenience.

my intention wasn't to make collection immutable.i just want to preserve one collection, just like backing up, you know, if anything goes wrong with the other. but both collections have the same array(using collection1.addAll(collection2)). if we can make a copy of the arrays in one collection and add it to the other, that can help. but the overhead involved in copying each array using for loop(in my case there could be some thousands of arrays)-bothering me a lot.

i have already tried clone. it's not working. is there any other way?

have a great day,
krishna prasad g
 
Campbell Ritchie
Marshal
Posts: 74387
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you clone? Did you clone the arrays or did you clone the ArrayList?

I am not at all sure that using a List is the best way to handle your problem, but am not sure what else to suggest. Anybody else?
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
with clone, we have to use for loop. as told before, that could be a overhead.

i tried cloning the collection. but it just works like this,

int[][] original = { {1,2},{2,3},.........,{2000,2001} };
int[][] clone = (int[][])original .clone();

think two dimensional arrays as arrays inside a collection.

any changes that i make with original, affects the clone as well.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
krisp, you're looking for complexity that just isn't there.

If you want a copy of the collection, then yes, you'd clone the collection. That's no different from creating a new one and using addAll(). In both cases, you get a new collection containing the same objects as the original.

What you want is a new collection containing copies of all the objects, and so there's no shortcut to simply copying all the objects.

Campbell was not telling you to clone the collection; he was telling you to clone each individual array in a for loop. It's no slower than using arraycopy(), and it's certainly clearer code, easier to read and write. But yes, you have to use a for loop. The "overhead" is irreducible: if you want to copy every element in the collection, then you need to visit every item in the collection and copy it. There's no way to avoid this.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default clone only does a shallow clone, meaning that the objects in the List are not copied but just referenced by two variables.

You can do a deep clone by various means. One is to use object serialization, another is to iterate through the List, create new objects derived from the original List and add them to a new List.
 
Campbell Ritchie
Marshal
Posts: 74387
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EFH and Roger Chung-Wee are correct. I was going to tell you that cloning the Collection or trying to clone all the arrays in one fell swoop will give you shallow clones, which means you can still change the values in the individual arrays. I agree with EFH that you will need a for loop to clone all the arrays into the new Collection.

And remember there is no such thing as a two-dimensional array; there are only one-dimensional arrays of one-dimensional arrays. So if you close an int[][] array you are only cloning the big array; the contained smaller arrays remain unchanged. A for loop will give much faster performance than serialisation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic