【问题标题】:Java. Array of objects爪哇。对象数组
【发布时间】:2011-01-07 05:51:02
【问题描述】:

问题是:我有这个代码:

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]

 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() 没有任何区别。这是我自己的,效果很好,我检查了。

谢谢!

【问题讨论】:

  • 您不应以大写字母开头的变量命名。请遵守 Java 约定。

标签: java arrays object


【解决方案1】:

这里有两个独立的概念:创建引用数组和创建类的实例。所以这一行:

AllComponents = new Component[nComponents]

将创建一个Component 引用数组。最初,所有引用都将为空。如果您想用对新实例的引用来填充它,则必须遵循该行:

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

编辑:要回复评论,AllComponents 是一个数组 - 它没有 ShiftsCouplings 的概念。如果您需要为新组件设置位移或联轴器,您应该使用:

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

或为Component 的构造函数添加参数以进行移位和耦合。

顺便说一句,我假设您是 Java 的新手,目前只想解决这个特定问题 - 当您准备好继续前进时,值得关注 Java coding conventions,并更稳健地封装您的数据。 (使用公共变量通常是个坏主意。)

【讨论】:

  • 明白谢谢。但更正 AllComponents = new Component[nComponents]; for (int iCounter=0;iCounter
  • 哎呀...对不起。我不知道如何放置格式良好的 cmets... :(
  • AllComponents 是一个数组 - 你需要 AllComponents[i].Shifts = ...AllComponents[i].Couplings = ...
【解决方案2】:
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 : 以大写开头的变量 == 不好的做法

【讨论】:

  • 我会说公开的变量是一个更严重的问题:)
  • 为什么?...我总是这样分隔它们... B 哪里有问题?附言感谢主 Q 的答案!
【解决方案3】:

试试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    相关资源
    最近更新 更多