2017-02-02 117 views
0

java初学者在这里!构造函数返回意外的null

我想要一个对象模型,它是随机坐标列表,并且根据(*),当主模型(200)运行时,ArrayList不是空的。但是,当我运行主我得到一个出界失误,当我与周围的打印测试(**)我得到的错误:

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

我相信你不是调用你的''paricleList''变量与''x1.get(0)''但超类的元素(你扩展ArrayList)。如果你想从''particleList''获得元素,你应该定义一个getter,然后执行如下操作:''x1.getParticleList()。get(0)''。 – Plebejusz

+0

你真的想扩展一个ArrayList吗? –

+0

你能扩展@BalajiKrishnan吗?我这样做是因为我想使用.get(i),但也许我应该使用Plebejusz提示。为什么我不想扩展ArrayList? –

回答

1

您正在混合实际上是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); 
} 
0

在循环,创造的Particle名单,我认为这将解决这个问题:

list1.add(new Particle()); 

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

1

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

} 
+0

太好了,谢谢! –