简体   繁体   English

构造函数返回意外的null

[英]Constructor returns unexpected null

java beginner here! Java初学者在这里!

I want an object Model that is a list of random coordinates, and according to (*) the ArrayList is not empty when the the main runs Model(200). 我想要一个对象模型,该对象模型是随机坐标的列表,并且根据(*),当主要运行Model(200)时ArrayList不为空。 But when I run main I get an out of bounds error when I test with the printout around (**) I get the error: 但是当我运行main时,当我在(**)周围打印输出进行测试时,出现了超出范围的错误:

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

Is the problem with the constructor Model? 构造函数模型有问题吗?

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

You are mixing up the array that Model actually IS because of the fact that it extends from ArrayList, and an ArrayList that your Model CONTAINS. 您正在混合使用Model实际存在的数组,因为它是从ArrayList扩展而来的,并且它是Model包含的ArrayList。

If you want your Model to be an ArrayList, you don't need that particleList property. 如果您希望模型成为ArrayList,则不需要那个particleList属性。 You could do this: 您可以这样做:

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

Your Model class is already an ArrayList. 您的Model类已经是一个ArrayList。 Your particleList field is useless. 您的particleList字段无用。

Try this : 尝试这个 :

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

In the loop that creating a list of Particle , I think this will solve the problem: 在创建Particle列表的循环中,我认为这将解决问题:

list1.add(new Particle());

And remove the line that instantiate the Particle object outside of the loop. 并在循环外删除实例化Particle对象的行。

In this case, there is no need to extend ArrayList (no new functionality is being added). 在这种情况下,无需扩展ArrayList(无需添加任何新功能)。 The below should work fine 下面应该可以正常工作

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