构造函数返回意外的null

时间:2017-02-02 14:56:49

标签: java arraylist constructor

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

4 个答案:

答案 0 :(得分:1)

你正在混合Model实际上是因为它从ArrayList扩展的数组,以及你的Model CONTAINS的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);
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

list1.add(new Particle());

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

答案 3 :(得分:0)

在这种情况下,不需要扩展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;
    }

}