Java的。对象数组 [英] Java. Array of objects
问题描述
问题是:我有一个code
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 ;
}
它的工作原理。
但是,当我尝试创建一个数组AllComponents [10]这一类组件的
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 ;
}
不copmile。
it does not copmile.
但是,如果我忽略了construstor的()
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...
还有什么比你的建议?
注:GetShifts()没有什么用非标准Java的getShift()。这是我自己的,它工作得很好,我检查。
What could you advice? Note: GetShifts() has nothing with a standart Java's getShift(). It is my own, it works well, i checked.
感谢名单!
推荐答案
这里有两个不同的概念:创建引用数组,并创建类的实例。所以这行:
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();
}
编辑:回复评论, AllComponents
是一个数组 - 它不具有班次的概念
或联轴器
。如果您需要为新组件的移位或联轴器,你应该使用:
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
}
或
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.
我假设你在Java中的新手,顺便说一句,只希望得到帮助,此刻这一特定问题 - 当你准备好继续前进,这将是值得看的Java编码规范和更有力封装数据。 (使用公共变量一般是一个坏主意。)
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.)
这篇关于Java的。对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!