home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

copying VERY large arrays with JAVA

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1595
Discussions: 200,988, Posts: 2,379,867, Members: 246,357
Old April 1st, 2003, 02:40 AM     #21 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
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
zskillz is offline   Reply With Quote
Old April 1st, 2003, 05:58 PM     #22 (permalink)
Member
 
DJDaveMark's Avatar
 
Join Date: Feb 2003
Location: France
Posts: 55
Unhappy

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
DJDaveMark is offline   Reply With Quote
Old April 1st, 2003, 10:59 PM     #23 (permalink)
Banned
 
qball's Avatar
 
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...
qball is offline   Reply With Quote
Old April 2nd, 2003, 03:38 PM     #24 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
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
zskillz is offline   Reply With Quote
Old April 2nd, 2003, 11:07 PM     #25 (permalink)
Banned
 
qball's Avatar
 
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...
qball is offline   Reply With Quote
Old May 17th, 2003, 08:43 AM     #26 (permalink)
Member
 
DJDaveMark's Avatar
 
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
DJDaveMark is offline   Reply With Quote
Old May 18th, 2003, 10:41 PM     #27 (permalink)
Banned
 
qball's Avatar
 
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...
qball is offline   Reply With Quote
Old May 19th, 2003, 12:41 AM     #28 (permalink)
Member
 
DJDaveMark's Avatar
 
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
DJDaveMark is offline   Reply With Quote
Old May 19th, 2003, 03:12 AM     #29 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
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
zskillz is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3066)
Charges against non-tippers dropped.. (20)
Health Care Rationing (11)
Delete an OS (17)
Nvidia GTX 260 problem (10)
Laptop with wireless problem. (12)
windows vista security holes (19)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (8)
windows 7 problem (7)
[F@H SPAM 11/16/09] ! 1/2 months to.. (39)
Internet Lost (5)
Recent Discussions
Jedi Academy Problem (3)
Desktop Calendar Application (1)
Can a page file be "too big".. (1)
Nvidia GTX 260 problem (10)
Point and Shoot Camera Suggestions. (8)
Looking for new motherboard (0)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (39)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)


All times are GMT -4. The time now is 06:49 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28