The problem is: i have this 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 compile.
But if I ignore the constructor’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 advise?
Note: GetShifts() has nothing with a standard Java’s getShift(). It is my own, it works well, i checked.
Thanx!
There are two separate concepts here: creating an array of references, and creating instances of your class. So this line:
will create an array of
Componentreferences. 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:EDIT: To reply to the comment,
AllComponentsis an array – it doesn’t have a concept ofShiftsorCouplings. If you need to set the shifts or couplings for the new component, you should use either:or
or add parameters to the constructor for
Componentto 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.)