繁体   English   中英

构造函数返回意外的null

[英]Constructor returns unexpected null

Java初学者在这里!

我想要一个对象模型,该对象模型是随机坐标的列表,并且根据(*),当主要运行Model(200)时ArrayList不为空。 但是当我运行main时,当我在(**)周围打印输出进行测试时,出现了超出范围的错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 

构造函数模型有问题吗?

public class Model extends ArrayList<Particle>{ 
private ArrayList<Particle> particleList;

public Model(int N) {
    ArrayList<Particle> list1 = new ArrayList<Particle>();
    Particle newParticle = new Particle(); 
    for(int i=0;i<N;++i){
        list1.add(newParticle);
        newParticle = new Particle();
        //String s= String.valueOf(newParticle.x);
        //System.out.println(s);
        }
    this.particleList = list1;
    Particle p1 = particleList.get(5);
    double d = p1.x;
    String s = String.valueOf(d);
    System.out.println(s);                            (*)
}

public static void main(String[] args){
    Model x1 = new Model(200);
    Particle p1 = x1.get(0);                           (**)
    double d = p1.x;
    String s = String.valueOf(d);
    System.out.println(s);
}

您正在混合使用Model实际存在的数组,因为它是从ArrayList扩展而来的,并且它是Model包含的ArrayList。

如果您希望模型成为ArrayList,则不需要那个particleList属性。 您可以这样做:

public class Model extends ArrayList<Particle> { 

  public Model(int N) {
      for (int i = 0; i < N; i++) {
          Particle newParticle = new Particle();
          this.add(newParticle);
      }
  }
}

public static void main(String[] args){
    Model x1 = new Model(200);
    Particle p1 = x1.get(0);                           (**)
    double d = p1.x;
    String s = String.valueOf(d);
    System.out.println(s);
}

您的Model类已经是一个ArrayList。 您的particleList字段无用。

尝试这个 :

public class Model extends ArrayList<Particle>{ 


public Model(int N) {

    for(int i=0;i<N;++i)
        this.add(new Particle());

    Particle p1 = this.get(5);
    double d = p1.x;
    String s = String.valueOf(d);
    System.out.println(s);                            (*)
}

public static void main(String[] args){
    Model x1 = new Model(200);
    Particle p1 = x1.get(0);                           (**)
    double d = p1.x;
    String s = String.valueOf(d);
    System.out.println(s);
}

在创建Particle列表的循环中,我认为这将解决问题:

list1.add(new Particle());

并在循环外删除实例化Particle对象的行。

在这种情况下,无需扩展ArrayList(无需添加任何新功能)。 下面应该可以正常工作

public class Model {
    private List<Particle> particleList = null;

    public Model(int count) {
        particleList = new ArrayList<>(count);
        for (int i = 0; i < count; i++)
            particleList.add(new Particle());
    }

    public static void main(String[] args) {
        Model x1 = new Model(300);
        Particle p = x1. getParticleList().get(0);
        System.out.println(p.x);
       //using the new method - no need to extend ArrayList
        System.out.print(x1.get(3));

    }

    //rather than extending Arraylist, you can create a new method and use get(index) like this one:
    public Particle get(int index){
        return this.getParticleList().get(index);
    }

    public List<Particle> getParticleList() {
        return particleList;
    }

    public void setParticleList(List<Particle> particleList) {
        this.particleList = particleList;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM