Jump to content
Mr. Green Gaming

Another problem with Java


MiF

Recommended Posts

Well i solved my last problem (some arrays stuff). Now i have new problem with arrays, my program would work after i fix this, but i cant find any solution.

Problem is those pointers with arrays. As programmers may know that if you just place array to other array, the new one points to that old one. So if you change information of one these arrays, data does change same way in all arrays that are pointed to / that point to that array (Not going 100% right this, but something like this)

Well my problem is copying an array. I have this method/function in Database class

public static String[][] getAllData() {
return dataArray; // original array
}

Then i call it like this

String[][] database = (String[][]) Database.getAllData().clone();
String[][] newDatabase = (String[][]) Database.getAllData().clone();

After i have done my stuff, all 3 arrays store same data.

I have tried to use system.arraycopy() method, but no luck with that either. I have tried to clone array in getAllData method with clone and arraycopy etc. NOTHING works.

Looking around internet wont help me. clone and arraycopy should work, but they are not on me.

What am i doing wrong?

Edited by MiF
Link to comment

Well if you clone an array of arrays, you get a new array that still has references to the same arrays. So basically you'll have to write your own function to copy all the sub-arrays too.

Pseudo code (since fuck looking up Java syntax again):


function cloneDatabase()
{
String[][] newArray

foreach( element in dataArray )
{
String[] newElement = element.clone()
newArray.insert( newElement )
}

return newArray
}

Link to comment

Well if you clone an array of arrays, you get a new array that still has references to the same arrays. So basically you'll have to write your own function to copy all the sub-arrays too.

I might be wrong, but it could be that you also need to clone each separate string object (because they are not primitive datatypes).

In that case, Clavus' code would only make copies of the arrays themselves, but the strings would still be the same.

Link to comment

Okey, got to test clavuses theory tommorow (or now).

Kikkers idea would be good, but this pointer thing affects only arrays and that stuff.

Clavuses idea did work. Thanks alot :worshippy:

/Can be locked now

Edited by MiF
Link to comment

Well if you clone an array of arrays, you get a new array that still has references to the same arrays. So basically you'll have to write your own function to copy all the sub-arrays too.

I might be wrong, but it could be that you also need to clone each separate string object (because they are not primitive datatypes).

In that case, Clavus' code would only make copies of the arrays themselves, but the strings would still be the same.

This is true BUT you can't change the String in one array and get the same result in the copied array, since you'd create a new String object and thus a new reference. A String object doesn't have methods to change its internal string value!

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...