copying VERY large arrays with JAVA  | | |
April 1st, 2003, 02:40 AM
|
#21 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
i understand the reference stuff, so I guess that my problem is "cloning" the new moldynPos array.... originally (as in the very first post of this thread) I was under the impression that I was already doing that. Would I have to manually go through and copy each value of the array to the new array in the constructor then? I think that there's a copy array method somewhere in the API actually, i'll have to look for that.
besides what I've said though, it seems as it might just be easier to avoid all of the cloning and just dereference the static method reference with a "= null;" line. That way I can't change the array unless I access it through the moldynPos object.
ideas?
-Z |
| |
April 1st, 2003, 05:58 PM
|
#22 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
|
Man....ur dangling a carrot infront of me.
I've got my NCC JavaScript exam coming soon and I've made a conscious decision not to look at any Java code. I'm burstin to look, fiddle and probe ur code but I really don't want to for now. Most exams try to catch you out by slipping in code from another language!
I've got a copy of it and will get back to you in the future if still needed, but looks to me like your in safe hands w qball.
choi 4 now, DaveMark |
| |
April 1st, 2003, 10:59 PM
|
#23 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
| Quote: |
Would I have to manually go through and copy each value of the array to the new array in the constructor then? I
| You could do that, but cloning an array should be easier. Quote: |
it might just be easier to avoid all of the cloning and just dereference the static method reference with a "= null;" line. That way I can't change the array unless I access it through the moldynPos object.
| That would orphan the reference to original object passed to moldynPos! Not what you desire.
I'll find a Array.clone() example to post back, else make a copy and pass... |
| |
April 2nd, 2003, 03:38 PM
|
#24 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
but when I pass it, I create a new reference to it in the moldynPos constructor. So if i then set it equal to null, the only reference that I have would be using the moldynPos object...
??
-Z |
| |
April 2nd, 2003, 11:07 PM
|
#25 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
|
You need to understand how java manipulates objects "by reference". Code: Button a = new Button("Okay");
Button b = new Button("Cancel");
a = b;
In the above code, line 1 creates a button object, so does line 2. Line 3 now sets 'a' to reference button 'b'. The okay button that 'a' used to refer to is lost/orphaned. (should be dereferenced and gc-ed?).
In your case (semi coded): Code: public static void main(String [] args)
{
Object a = new Object();
method (a);
a=null;
}
public void method (Object arg)
{
//arg is just a reference to object a!
//use and manipulate arg
//if instance, you want some sort of persistence
}
In the above case, once "a=null;" is executed, the arg reference is null also!
vs this code: Code: public static void main(String [] args)
{
Object a = new Object();
b = a.clone();
method (b);
a=null;
}
public void method (Object arg)
{
//arg is just a reference to object b
}
Now a is dereferenced, but you still have the object 'b' in scope, not exactly what you want. This may work better: Code: public static void main(String [] args)
{
Object a = new Object();
method (a);
a=null;
}
public void method (Object arg)
{
b = arg.clone();
//now you have copy (NOT reference) of arg in b
//a can be dereferenced, and b still in scope of method
}
Back to your original problem of having arrays hogging resources. Have your instance of moldynPos object have a method that creates and manipulates the arrays WITHIN the scope of the method. Not a static var passed in constructor, or to method. Don't pass the arrays, call the object/method and have THAT create arrays within the object/method scope.
This can be a little confusing, so I hope it helps... |
| |
May 17th, 2003, 08:43 AM
|
#26 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
| Quote: Originally posted by zskillz I tried making an object of my main class and then
populating the arrays that way, but then I started thinking
about it, and it turns out, that I'm not even creating a
brand new array when I execute the line: Code: position = new moldynPos(z.pos_array);
| Don't know how i missed this but aren't you just looking to do this
instead of the above quoted code.... Code: //create a new empty array the same size as z.pos_array
int [] arr = new int[z.pos_array.length];
//efficiently copy the values from one array to another
System.arraycopy(z.pos_array, 0, arr, 0, z.pos_array.length);
//pass in the new array instead
position = new moldynPos(arr);
Java passes arrays by reference since they're objects hence
no brand new array. But using the System.arraycopy() method
sends in your newly copied array.
Let me know if this helps!
DaveMark |
| |
May 18th, 2003, 10:41 PM
|
#27 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
|
That would work for arrays, but other objects???
Still need to deal with reference to:
int [] arr = ...
arr will be in scope that executes:
position = new moldynPos(arr);
not within the object moldynPos... |
| |
May 19th, 2003, 12:41 AM
|
#28 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
|
OK, are you still able to upload your text files & stuff somewhere, I want
to have a bash at this with all the nitty gritty.
C'est possiblé?
I'll be able to upload any changes to my free web space.
DaveMark |
| |
May 19th, 2003, 03:12 AM
|
#29 (permalink)
| | Senior Member
Join Date: Oct 2001
Posts: 881
|
are you speaking to me???
I can probably just email them to you if you would like... pm me and we'll figure it out
-Z |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |