简体   繁体   中英

Constructor returns unexpected null

java beginner here!

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). But when I run main I get an out of bounds error when I test with the printout around (**) I get the error:

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.

If you want your Model to be an ArrayList, you don't need that particleList property. 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. Your particleList field is useless.

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:

list1.add(new Particle());

And remove the line that instantiate the Particle object outside of the loop.

In this case, there is no need to extend ArrayList (no new functionality is being added). 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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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