views:

235

answers:

3

Hi!

The problem is: i have a code

public class Component {
public Component() {
// TODO Auto-generated constructor stub
 }
 public double[] Shifts ;
 public double[][] Couplings ;

}

public class Decouplage {
 public Decouplage(double[] GroupShifts, double[][] GroupCoup) {
  AllComponents = new Component(); 
  AllComponents.Shifts = GetShifts(...blah-blah-bla...);
  AllComponents.Couplings = GetGouplings(...blah-blah-bla...);
 }

public Component AllComponents ; 
}

It works. But when I try to create an array AllComponents[10] of this class Component

 public class Decouplage {
 public Decouplage(double[] GroupShifts, double[][] GroupCoup) {
  AllComponents = new Component()[nComponents]; /////HOW MUST I PUT IN ONE LINE THE NUMBER OF ELEMENTS AND THE () FOR CONSTRUCTOR????
  for (int iCounter=0;iCounter<nComponents;iCounter++){
         AllComponents.Shifts = GetShifts(...blah-blah-bla...);
         AllComponents.Couplings = GetGouplings(...blah-blah-bla...);
  }
 }
public Component[] AllComponents ; 
}

it does not copmile.

But if I ignore the construstor's ()

public class Decouplage {
 public Decouplage(double[] GroupShifts, double[][] GroupCoup) {
  AllComponents = new Component[nComponents]; /////IS IT LEGAL TO IGNORE CONSTRUCTOR, EVEN IF IT IS EMPTY????
  for (int iCounter=0;iCounter<nComponents;iCounter++){
         AllComponents.Shifts = GetShifts(...blah-blah-bla...);
         AllComponents.Couplings = GetGouplings(...blah-blah-bla...);
  }
 }
public Component[] AllComponents ; 
}

it can not resolve Shifts and Couplings as a fields...

What could you advice? Note: GetShifts() has nothing with a standart Java's getShift(). It is my own, it works well, i checked.

Thanx!

+5  A: 

There are two separate concepts here: creating an array of references, and creating instances of your class. So this line:

AllComponents = new Component[nComponents]

will create an array of Component references. Initially, all the references will be null. If you want to fill it with references to new instances, you'll have to follow that line with:

for (int i = 0; i < nComponents; i++)
{
    AllComponents[i] = new Component();
}

EDIT: To reply to the comment, AllComponents is an array - it doesn't have a concept of Shifts or Couplings. If you need to set the shifts or couplings for the new component, you should use either:

for (int i = 0; i < nComponents; i++)
{
    AllComponents[i] = new Component();
    AllComponents[i].Shifts = // Code here
    AllComponents[i].Couplings = // Code here
}

or

for (int i = 0; i < nComponents; i++)
{
    Component component = new Component();
    component.Shifts = // Code here
    component.Couplings = // Code here
    AllComponents[i] = component;
}

or add parameters to the constructor for Component to take the shifts and couplings.

I'm assuming you're a newbie at Java, by the way, and only want help on this specific problem at the moment - when you're ready to move on, it would be worth looking at Java coding conventions, and encapsulating your data more robustly. (Using public variables is generally a bad idea.)

Jon Skeet
Understood thanks.but the correction to AllComponents = new Component[nComponents]; for (int iCounter=0;iCounter<nComponents;iCounter++){ AllComponents[iCounter] = new Component(); AllComponents.Shifts = GetShifts(...); AllComponents.Couplings = GetGouplings(...); }Did not help. Still: the error on the lines of "Shift" and "Couplings": AllComponents.Shifts cannot be resolved or is not a field AllComponents.Couplings cannot be resolved or is not a fieldTell me please if i need to put smt in constructor of Component class?Thank you
Andrew
ooops... soryy. I dont know how to put well-formatted comments... :(
Andrew
AllComponents is an array - you need `AllComponents[i].Shifts = ...` and `AllComponents[i].Couplings = ...`
Jon Skeet
Oh, that's my stupidity, the last :)thanx !
Andrew
+1  A: 
AllComponents = new Component[nComponents];
 for (int iCounter=0;iCounter<nComponents;iCounter++){
   AllComponents[iCounter] = new Component();
   AllComponents[iCounter].Shifts = GetShifts(...blah-blah-bla...);
   AllComponents[iCounter].Couplings = GetGouplings(...blah-blah-bla...);

ps : variable starting with uppercase == bad practice

Maxime ARNSTAMM
I'd say that the variables being public is a more serious problem :)
Jon Skeet
Why?...I was always separating them like this... Where can B a problem?P.S. Thanx for the answer for main Q!
Andrew
+1  A: 

Try:

   public Decouplage(double[] GroupShifts, double[][] GroupCoup) {
      Component AllComponents[] = new Component[nComponents];
      for (int i = 0; i < AllComponents.length; i++) {
          AllComponents[i] = new Component();
          AllComponents[i].Shifts = GetShifts();
          AllComponents[i].Couplings = GetGouplings();
    }
MatthieuF
Thank you!Seems it works
Andrew