C
Clint Johnson
Hello everyone,
I'm at my wits end trying to figure this one out. I have this:
public class FirstClass {
....
public class SecondClass{ int n; }
public class ThirdClass{
SecondClass myArray[]=new SecondClass[7];
Init() { for(i=0;i<7;i++} myArray=new SecondClass();
myArray.n=0; }
void SomeFunction() { myArray[0].n=5; }
}
}
This is a sketch of my basic structure. The baffling thing is, when
the last statement executes, instead of changing n at myArray[0], it
changes it for ALL 7 elements. Does anyone have any idea why this is
happening?
I have identical arrays in the SecondClass (myArray[] and myArray2[],
for example). One of them works properly and one of them does not. I
had the problem with another array in the same class before and I
"fixed" it by explicitly initializing the arrays in a function
(instead of the constructor).
Anyway, I'm sure there's something screwy going on here, maybe a
memory thing.. but after working on it for hours, I still haven't a
clue. If anyone can help with this, I would most definitely appreciate
it!
Clint
I'm at my wits end trying to figure this one out. I have this:
public class FirstClass {
....
public class SecondClass{ int n; }
public class ThirdClass{
SecondClass myArray[]=new SecondClass[7];
Init() { for(i=0;i<7;i++} myArray=new SecondClass();
myArray.n=0; }
void SomeFunction() { myArray[0].n=5; }
}
}
This is a sketch of my basic structure. The baffling thing is, when
the last statement executes, instead of changing n at myArray[0], it
changes it for ALL 7 elements. Does anyone have any idea why this is
happening?
I have identical arrays in the SecondClass (myArray[] and myArray2[],
for example). One of them works properly and one of them does not. I
had the problem with another array in the same class before and I
"fixed" it by explicitly initializing the arrays in a function
(instead of the constructor).
Anyway, I'm sure there's something screwy going on here, maybe a
memory thing.. but after working on it for hours, I still haven't a
clue. If anyone can help with this, I would most definitely appreciate
it!
Clint