Copying arrays from one class to another
I'm trying to create a method in one class to fill up an array with double values, and then I not only need to call that method in another class, but I need to be able to assign those double values to a new array in the second class. I have to be able to iterate through the double values and subtract them from a total, and so far I haven't found a good way to do that. Is it possible to copy an array into an array in a new class, or do I need some other sort of trick?
Thanks!
Re: Copying arrays from one class to another
Quote:
Is it possible to copy an array into an array in a new class,
Yes. What you need to consider is if you want a copy of the contents of the array or if you just need a reference to the one array to be able to access that array from another class.
Re: Copying arrays from one class to another
That's the thing, I know how to access an array from another class, but I need to actually copy the contents, and I can't figure out how to do that.
Re: Copying arrays from one class to another
Quote:
how to access an array from another class
Do you know how to access a variable in one class from another class? An array is just another type of variable.
Quote:
copy the contents,
There are several ways:
use a loop to copy element by element
use the clone() method: newArray = oldArray.clone()
use the Arrays class's copyOf() method