• 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

Backup of array

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i have a code:



why change the arrayTemp? how / where to store original array1 that I had created a backup?
thank you for answer
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I buy a car. I say "This is my car"
My daughter says, this is my dad's car.
Both are referring to the same car.

A bird comes and sits on the car.

I say, a bird is sitting on my car.
My daughter says, a bird is sitting on my dad's car.
Both are still referring to the same car.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, variables of array types are references, not the arrays themselves.

So, variables in your program such as array1 and arrayTemp are only references to the actual array. In a line like this:

you are not making a copy of the array - you are only creating a new reference to the same array.
 
Miroslav Benko
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys now I understand it.
I solved it by gradually filling the array over the cycle for.
Thank you again
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try newArray = oldArray.clone(); but that gives you a shallow clone so for an array of arrays you would have to clone each member array too. That will not be completely reliable if the array contains mutable reference types.
There is probably some λ which will do that in Java8 but I don't know enough about it. The Arrays class has some copy methods but I am not sure they will help you. I suspect this will be no better.
 
reply
    Bookmark Topic Watch Topic
  • New Topic