Resetting an array.
I need to create a new object which uses the same array as a current object. from what i can see, the below SHOULD work, but it doesn't:
public Board makeCopy(){
Board copy =new Board(initialNum);
copy.setBoardArray(numArray);
return copy;
}
and:
publicvoid setBoardArray(int[] newArray){
this.numArray = newArray;
}
but when i perform operations on say object 1's array, the same thing happens in object 2's array!
of course it does, they're the same object
but im creating a new object with the new keyword arent i?!?
> but im creating a new object with the> new keyword arent i?!?a new object. you're passing a single array around, though. passing an array into a method doesn't copy the array
> but im creating a new object with the
> new
keyword arent i?!?
You create a separate Board object, but you don't create a new array. You end up with two Boards that both point to the same array. You need to create a new array and copy the values into the new array.
int[] newarray = new array[oldarray.length];
for (int i=0; i<newarray.length; i++) {
newarray[i] = oldarray[i];
}
>
thanks! forgot an object was an array and needed copying too!
> thanks! forgot an object was an array and needed> copying too!hopefully you really meant that arrays are objects...by the way, if you need a new array for each instance of your other object, why are you bothering to pass in an existing one in the first place?
>> You need to create a
> new array and copy the values into the new array.
> int[] newarray = new array[oldarray.length];
> for (int i=0; i<newarray.length; i++) {
>newarray[i] = oldarray[i];
Hunter, you know much better than that.
int[] newarray = new int[oldarray.length]
System.arraycopy(oldarray, 0, newarray, 0, oldarray.length);
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
> Hunter, you know much better than that.
>
> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Syst
> em.html
>
> [code]
> static void arraycopy(Object src, int srcPos, Object
> dest, int destPos, int length)
> Copies an array from the specified source
> array, beginning at the specified position, to the
> specified position of the destination array.
> /code]
True, I would use that method in production code, but for the OP's benefit, I think showing them the "hard way" helps make it clearer how it works. One of the reasons I hang around here is that I really like teaching people. In fact I've been contemplating becoming a TA when I go back to school, except that I learned the first time around that I really just don't like college kids, even when I was one. :-)
> True, I would use that method in production code, but
> for the OP's benefit,
Dont mind me I just have a love affair with arraycopy, lol.
It seems like the little known or talked about method.
> One of the
> reasons I hang around here is that I really like
> teaching people.
Yea I think you would have to.
Otherwise why would anyone stick around to help some of the mutatns here, lol.
Ive been here for 2+ years. (Thats depressing.)
> I learned the first time around that I really just
> don't like college kids, even when I was one. :-)
Haha. I disagree. Once you leave college you learn quickly that
women will never be as cool as they were then.
Earning a degree seems to age woman 10 years.
Is that a horrible thing to say?