JAVA对象数组
问题是:我有以下代码:JAVA对象数组,java,arrays,object,Java,Arrays,Object,问题是:我有以下代码: 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)
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 ;
}
它起作用了。
但是当我试图创建一个数组时,这个类的所有组件[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 ;
}
公共类卸载{
公共解除上载(双[]组移位,双[]组耦合){
AllComponents=new Component()[nComponents];//我必须如何将构造函数的元素数和()放在一行中????
对于(int-iCounter=0;iCounter,这里有两个独立的概念:创建引用数组和创建类的实例
AllComponents = new Component[nComponents]
将创建一个组件
引用数组。最初,所有引用都将为空。如果要用对新实例的引用填充该数组,则必须遵循该行:
for (int i = 0; i < nComponents; i++)
{
AllComponents[i] = new Component();
}
或
for(int i=0;i
或者将参数添加到组件的构造函数中
以进行移位和耦合
顺便说一句,我假设你是Java新手,目前只想在这个特定问题上得到帮助——当你准备继续前进时,它会值得一看,并更可靠地封装你的数据。(使用公共变量通常是个坏主意。)AllComponents=newcomponent[ncomonents];
对于(int iCounter=0;iCounter请尝试:
public Decouplage(双[]groupshift,双[]GroupCoup){
组件AllComponents[]=新组件[nComponents];
对于(int i=0;i
您不应该以大写字母开头命名变量。请遵循Java惯例。我想说的是,变量公开是一个更严重的问题:)为什么?…我总是这样把它们分开…哪里有问题呢?P.S.Thanx为主要问题的答案!明白了,谢谢。但是对AllComponents=new Components[nComponents];for(int-iCounter=0;ICounteroops…soryy.我不知道如何放置格式良好的注释…:(AllComponents是一个数组-您需要AllComponents[i]。移位=…
和AllComponents[i]。耦合=…
for (int i = 0; i < nComponents; i++)
{
AllComponents[i] = new Component();
}
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;
}
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...);
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();
}