MiF Posted July 23, 2011 Posted July 23, 2011 (edited) 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 classpublic static String[][] getAllData() { return dataArray; // original array }Then i call it like thisString[][] 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 July 23, 2011 by MiF Quote
Clavus Posted July 23, 2011 Posted July 23, 2011 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} Ywa 1 Quote
Kikkers Posted July 23, 2011 Posted July 23, 2011 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. Quote
MiF Posted July 23, 2011 Author Posted July 23, 2011 (edited) 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 /Can be locked now Edited July 23, 2011 by MiF Quote
Clavus Posted July 23, 2011 Posted July 23, 2011 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! Ywa 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.