Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Passing Array Parameters

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that when an array is passed to a class or method the value of its reference is sent. Hence when I pass an array to a new class/object I created and that object performs operations on that array, the values are changed in the original array. How do I pass an array so that the object creates a local copy and the original isn't altered by the new object/class/method?

ex.

private int naPoo[][] = null;

foo(int[][] p_naPoo)
{
naPoo = p_naPoo;
naPoo[1][1] = 69;
}

main()
{
int naPooOrig[][] = new int[2][2];
naPooOrig[1][1] = 77;
foo objFoo = new foo(naPooOrig);
System.out.println(naPooOrig[1][1]);
}

this will give output of 69. I don't want this. I want to pass the array but not have it altered by the receiving object or method.

I tried using:

naPoo = (int[][]) p_naPoo.clone();

but that doesn't work... same result. Any help would be much appreciated.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Micho Lee:

I tried using:

naPoo = (int[][]) p_naPoo.clone();

but that doesn't work... same result. Any help would be much appreciated.



Yes, it worked - just not the way you expected...

The problem is that you don't just have an array, but that you have an array of arrays. What you cloned was just the outer array - the clone still referenced the same inner arrays. What you need to do is to clone those, too. Unfortunately, to my knowledge there is no shortcut to do that...
 
Micho Lee
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm guessing I just have to manually copy any multi-dimentional arrays that are passed if I don't want any references to the original?

For now that's what I did but I wish there were some shortcut.

BTW thanks for the reply.
 
Marshal
Posts: 28289
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if you'd call it a shortcut, but putting your data in some kind of immutable class that you designed for the purpose might make more sense than struggling against the capabilities of arrays.
reply
    Bookmark Topic Watch Topic
  • New Topic