两种方法使用相同的ArrayList [英] Use a same Arraylist in two methods

查看:118
本文介绍了两种方法使用相同的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class ObjetTWS {

    String nom;
    List<String> jobAmont = new ArrayList<String>();
    List<String> jobAval = new ArrayList<String>();
    String type;

    public ArrayList<ObjetTWS> dependances;

    public ObjetTWS() {

    }

    public ObjetTWS(String p_nom, String p_type, String p_jobAmont,
            String p_jobAval) {

        ObjetTWS obj = new ObjetTWS();

        obj.nom = p_nom;
        obj.jobAmont.add(p_jobAmont);
        obj.jobAval.add(p_jobAval);
        obj.type = p_type;

        dependances = new ArrayList<ObjetTWS>();
        dependances.add(obj);

    }

    public void ajouterJob(String p_nom, String p_jobAmont, String p_jobAval) {

    }

}

在ObjetTWS(...),我填充对象ObjetTWS我的列表。的对象与p_nom,p_type,...的从ObjetsTWS(...)的值创建。我需要后收回此列表中ajouterJob添加一些其他的信息。但我有列表中只有第一个对象dependances。我怎样才能把所有的名单?

In ObjetTWS(...), I fill my list with the objects ObjetTWS. The objects are created with the values of p_nom,p_type,... from ObjetsTWS(...). I need after take back this list in ajouterJob to add some other information. But I have the list dependances with just the first object. How I can take all the list ?

推荐答案

在您的code每 ObjectTWS 都有它自己的 dependances 列表。因此,似乎在逻辑上,你总是列表中只包含一个元素。如果你需要的所有对象的列表,使 dependances 静态变量。

In your code every ObjectTWS has it's own dependances list. So it seems logically that your list always contains only one element. If you need a list of all objects, make dependances variable static.

这篇关于两种方法使用相同的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆