array of classes - one change hits all - help!

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
 
E

Eric Sosman

Clint said:
Hello everyone,

I'm at my wits end trying to figure this one out. I have this:
[pseudocode snipped]

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? [...]

"Doctor, I don't feel so good."

"What's the problem?"

"I'm not going to tell you, but I'll give you a
sketch of the basic complaint."

"Take two aspirin and call me in the morning."

In other words, providing a mere "sketch" isn't enough.
Provide concise, complete, compilable code that demonstrates
the difficulty, and someone will probably spot the problem.
Quite often the "someone" will in fact be you yourself; the
process of discarding the extraneous stuff and whittling the
problem down to its essentials is an excellent way to focus
your attention.

We don' need no steenkin' paraphrases!
 
P

Peter Kirk

Clint Johnson said:
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?


There do appear to be some errors in the code you posted, and it is a bit
difficult to see exactly what it is expect. But try this:

public class ArrSet{
public class SecondClass{
int n;
}

public class ThirdClass{
SecondClass myArray[] = new SecondClass[7];

public void init(){
for ( int i = 0; i < 7; i++ ){
myArray = new SecondClass();
}
outArray();
}

public void someFunction(){
myArray[1].n = 5;
outArray();
}

private void outArray(){
for ( int i = 0; i < 7; i++ ){
System.out.println( "SecondClass[ " + i + " ].n = " +
myArray.n );
}
}
}


public static void main( String[] args ){
ArrSet as = new ArrSet();
ThirdClass tc = as.new ThirdClass();
tc.init();
tc.someFunction();
}
 
B

Bryce

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 code won't compile. There is absolutely no way this can be
debugged the way its presented above.

What I try to do is, if something is presenting me problems, try to
find the simplest way to isolate it in a test class. The post that
here if you are still have problems.
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
 
A

Andrew Thompson

Provide concise, complete, compilable code that demonstrates
the difficulty, and someone will probably spot the problem.
Quite often the "someone" will in fact be you yourself; the
process of discarding the extraneous stuff and whittling the
problem down to its essentials is an excellent way to focus
your attention.

Well stated Eric, ..more verbously expressed in..
We don' need no steenkin' paraphrases

Yeah, yeah... The 'cute catchy phrase' helps as well.. ;-)

To the OP, *please* do NOT x-post between
c.l.j.programmer and c.l.j.help!

The response this provokes from regular posters
to ..either/both groups are *very* different..

[ As a 'for instance', I am often prompted to submit
short.. sharp responses to c.l.j.p. that would be
*completely* inapppropriate for c.l.j.help. And,
in case you're wondering, yes, I have been told so
in public *and* in private. ]

So, make your choice, c.l.j.help, much more information,
delivered with a great deal more care (especially for
your feelings) or c.l.j.programmer, where you encounter
people like me ..at their worst. Please choose one or
the other. [ 'cos I do not like c.l.j.h.'s hearing what
they are in for, when they hit c.l.j.p. ;-) ]

[ Due to the fact my Newsreader whines at me for
cross-posting, I am directing the follow-ups to this
thread to c.l.j.help ONLY.. ]

I ..really will be nicer, ..there. No, ..REALLY!
 
T

tom bender

If n is in the outer class then all the inner class objects will
reference the same n value.
 
C

Clint Johnson

I appreciate the help, guys.. I know the code I posted won't compile.
The program that has the problem is a few thousand lines long and I
figured this was the next best thing.

The ArrSet code is exactly what I was talking about. That's my code in
a nutshell.

After cropping my program down about 99% and adding a main(), it works
as it should. This is the short, working version.

This is especially frustrating since it worked when I took it apart.

Let me ask a more pointed question: if I have an array of classes,
what can I do wrong so that modifying one integer in one part of the
array modifies the integer for all parts of the array? Is the rest of
the array being cleared to null somehow?




----test.java-----

import java.awt.*;
import java.awt.event.*;

public class test {
private AllCardsClass c = new AllCardsClass();

void load(){
c.Initialize();
}

void try_it(){
c.someFunction(5);
for(int i=0;i<7;i++) System.out.println("("+i+") =
"+c.oc2.n);
}

public class CardClass {
public int n=-1;
void CardClass(){}
void reset(){n=-1;}
void change(int num){reset(); n=num; }
}

public class AllCardsClass {
public CardClass pc[]=new CardClass[10];
public CardClass oc[]=new CardClass[10];
public CardClass pc2[]=new CardClass[7],oc2[]=new CardClass[7];
AllCardsClass(){}
void Initialize() {int q;
for(q=0;q<10;q++) { pc[q]=new CardClass(); pc[q].reset();
oc[q]=new CardClass(); oc[q].reset(); }
for(q=0;q<7;q++) { pc2[q]=new CardClass(); pc2[q].reset();
oc2[q]=new CardClass(); oc2[q].reset(); }
}
void someFunction(int num){oc2[3].change(num); }

}

public static void main(String[] args){
test t = new test();
t.load();
t.try_it();
}
}
 
E

Eric Sosman

Clint said:
I appreciate the help, guys.. I know the code I posted won't compile.
The program that has the problem is a few thousand lines long and I
figured this was the next best thing.

The ArrSet code is exactly what I was talking about. That's my code in
a nutshell.

After cropping my program down about 99% and adding a main(), it works
as it should. This is the short, working version.

This is especially frustrating since it worked when I took it apart.

Let me ask a more pointed question: if I have an array of classes,
what can I do wrong so that modifying one integer in one part of the
array modifies the integer for all parts of the array? Is the rest of
the array being cleared to null somehow?
[code snipped]

When I run it, the output I get is

(0) = -1
(1) = -1
(2) = -1
(3) = 5
(4) = -1
(5) = -1
(6) = -1

.... which doesn't suggest (to me, anyhow) any kind of "change
one changes all" behavior. The array is originally populated
with objects that have the value -1 in a data member, then one
of those objects' data is changed to 5, and then all are
printed out -- and lo! only the chosen objects' value has
changed. What do you think is incorrect or puzzling about this?
 
J

John C. Bollinger

Clint said:
Let me ask a more pointed question: if I have an array of classes,
what can I do wrong so that modifying one integer in one part of the
array modifies the integer for all parts of the array? Is the rest of
the array being cleared to null somehow?

Being a bit pedantic, you cannot ever have an array of classes, only an
array of objects (or of primitives). It helps to keep the language
clear so that everyone understands what is said.

The most likely problem is that the array positions all contain
references to the same object. I couldn't say how that situation might
come to pass in your particular application, but the condition is easy
enough to test. Another possibility would be that you are manipulating
static state rather than instance state, but that does not appear to be
the case. Any other possibility I can imagine would depend on some kind
of machinery that updated all the objects in response to an operation on
one of them.


John Bollinger
(e-mail address removed)
 
D

Dave

Let me ask a more pointed question: if I have an array of classes,
what can I do wrong so that modifying one integer in one part of the
array modifies the integer for all parts of the array? Is the rest of
the array being cleared to null somehow?

My first guess is that you are somehow filling out your array with
references to the same object. Or you are somehow modifying the array
so that all elements end up referencing the same object.

Check in a debugger, or printout the elements of the array and see if
they really all refer to the same instance.

Another possibility is that the field that's being modified has class
scope and so there would be only a single instance of that field
regardless of how many instances of the object that contains it there
might be.
 
C

Clint Johnson

Thanks Tom! Although that wasn't precisely the problem, you led me to
it. As it turns out, if you have an assignment like:

oc2[3]=oc2[4]

instead of

oc2[3].n=oc2[4].n

then.. surprisingly.. it affects all of the oc2[] array.. bizarre...
oh well.. a great weight has been lifted from my proverbial shoulders.
Thanks to all!

Clint


tom bender said:
If n is in the outer class then all the inner class objects will
reference the same n value.

Clint said:
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
 
R

Ryan Stewart

Clint Johnson said:
Thanks Tom! Although that wasn't precisely the problem, you led me to
it. As it turns out, if you have an assignment like:

oc2[3]=oc2[4]

instead of

oc2[3].n=oc2[4].n

then.. surprisingly.. it affects all of the oc2[] array.. bizarre...
oh well.. a great weight has been lifted from my proverbial shoulders.
Thanks to all!
Not surprising at all. It's what was suggested by others in this thread. The
statement
oc2[3] = oc2[4]

causes both elements of the array to reference the same object. You might
want to have a look at
http://java.sun.com/docs/books/tutorial/
 
T

Thomas G. Marshall

Bryce said:
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 code won't compile. There is absolutely no way this can be
debugged the way its presented above.

What I try to do is, if something is presenting me problems, try to
find the simplest way to isolate it in a test class. The post that
here if you are still have problems.


*COMPILE IT FIRST*, and then post it here if you still have problems.

Untested code within newsgroups is A OK. But not when you are asking about
a particular problem. It just wastes everyone's time.

....[slash]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top