java 爪哇。对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2100991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java. Array of objects
提问by Andrew
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
有用。但是当我尝试创建此类 Component 的数组 AllComponents[10] 时
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...
它无法将 Shifts 和 Couplings 解析为字段...
What could you advise? Note: GetShifts() has nothing with a standard Java's getShift(). It is my own, it works well, i checked.
你有什么建议?注意:GetShifts() 与标准 Java 的 getShift() 没有任何关系。这是我自己的,效果很好,我检查过。
Thanx!
谢谢!
回答by Jon Skeet
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 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:
将创建一个Component引用数组。最初,所有引用都将为空。如果你想用对新实例的引用来填充它,你必须遵循该行:
for (int i = 0; i < nComponents; i++)
{
AllComponents[i] = new Component();
}
EDIT: To reply to the comment, AllComponentsis an array - it doesn't have a concept of Shiftsor Couplings. If you need to set the shifts or couplings for the new component, you should use either:
编辑:要回复评论,AllComponents是一个数组 - 它没有Shifts或的概念Couplings。如果您需要为新组件设置移位或耦合,您应该使用:
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 Componentto take the shifts and couplings.
或向构造函数添加参数以进行Component移位和耦合。
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 的新手,目前只需要有关此特定问题的帮助 - 当您准备继续前进时,值得查看Java 编码约定,并封装您的数据更稳健。(使用公共变量通常是一个坏主意。)
回答by Maxime ARNSTAMM
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
ps:以大写开头的变量==不好的做法
回答by Matthew Farwell
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();
}

