Java. 对象数组

4
问题是:我有这段代码:
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]的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 ; 
}

它无法编译。

但是如果我忽略构造函数中的 ()

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 ; 
}

它不能将Shifts和Couplings解析为字段...

你有什么建议吗? 注意:GetShifts()与标准Java的getShift()无关。这是我的自己写的,它运行良好,我已经检查过。

谢谢!


3
你不应该以大写字母开头命名变量。请遵循Java约定。 - Iker Jimenez
3个回答

12
这里有两个独立的概念:创建引用数组和创建类的实例。因此,这一行代码是这样的:
AllComponents = new Component[nComponents]

将创建一个Component引用数组。最初,所有的引用都将为null。如果您想用新实例的引用填充它,您需要在该行后面跟着:

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

编辑:针对评论进行回复,AllComponents是一个数组 - 它没有“Shifts”或“Couplings”的概念。如果您需要为新组件设置移位或耦合,您应该使用以下方法之一:

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;
}

或者向组件的构造函数添加参数以接受移位和耦合。
顺便说一下,我假设你是Java的新手,目前只想在这个特定的问题上获得帮助 - 当你准备好继续时,值得看一下Java编码规范,并更加牢固地封装你的数据。(通常使用公共变量是不好的。)

明白了,谢谢。但是对于以下更正: AllComponents = new Component[nComponents]; for (int iCounter=0;iCounter<nComponents;iCounter++){ AllComponents[iCounter] = new Component(); AllComponents.Shifts = GetShifts(...); AllComponents.Couplings = GetGouplings(...); }并没有起到作用。 仍然出现了“Shift”和“Couplings”的错误: AllComponents.Shifts无法解析或不是字段 AllComponents.Couplings无法解析或不是字段请告诉我是否需要在Component类的构造函数中添加内容?谢谢 - Andrew
抱歉,我不知道如何放置格式良好的注释... :( - Andrew
AllComponents是一个数组 - 你需要AllComponents[i].Shifts = ...AllComponents[i].Couplings = ... - Jon Skeet

1
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...);

提示:以大写字母开头的变量 == 不良实践


1
我认为变量是公共的是一个更严重的问题 :) - Jon Skeet
为什么?我总是这样分开它们...B可能出了什么问题?附言:感谢您对主要问题的回答! - Andrew

1

尝试:

   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();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接