2013-03-14 84 views
0

我正在研究一个类,它是后端类视图,需要显示一些后端类属性。在ArrayList中保留项目

public abstract class AGenericNodeView{ 
    private ANode fObject; 
    private ArrayList<TInputView> fInputs = null; 

    public AGenericNodeView(){ 
     super(); 
     this.fInputs = new ArrayList<TInputView>(); 
    } 


    private void getInputs(){ 
     int num = fObject.getNumInputs(); //Number of inputs 
     for (int i = 0; i < num; i++) 
     { 
      this.fInputs.add(new TInputView()); 
      this.fInputs.get(i).doSth(); 
     } 
     //fInputs has now the size num which is greater than 0. 
    }  

    //later I call: 
    public draw() 
    { 
     //... 
     log(this.fInputs.size()); //output fInputs.size() 
     // and it is 0. Always. 
    } 


} 

我想,如果有一种方法,让我创建的对象在getInputs()执着,所以该列表不是空的,需要draw()什么时候?

+0

在您使用它的地方添加相关的代码。 – BobTheBuilder 2013-03-14 13:40:08

+0

您确定要调用getInputs()吗?你在同一个实例上调用getInputs()和draw()?因为它应该是“持久的”。 – 2013-03-14 13:40:10

+1

“*,它是0. Always。*”=>看起来'draw'在'getInputs'之前被调用,那么......只有其他的可能性是如果你有两个不同的线程执行'draw'和'getInputs'。 – assylias 2013-03-14 13:40:21

回答

1

一对夫妇的建议:

  1. 使用调试器。

  2. 登录numgetInputs

  3. 登录System.identityHashCode(fInputs)getInputsdraw并验证它们是相同的。

+0

好。我发现,我正在处理包含类的两个不同的对象 - 我会尽力解决这个问题,也许稍后再提及你们。万分感谢! – 2013-03-14 14:08:36

+0

而这实际上是唯一的问题。解决! – 2013-03-14 14:18:36

+0

如果这解决了你的问题,你应该接受答案。 – 2013-03-15 13:55:35