返回错误数据的数组列表



我目前正在制作一个android应用程序,该应用程序需要一些数据暂时作为数组列表(字符串)的数组列表保存,但是当试图让我的头绕着它们时,因此添加虚拟数据并将其打印到depug流,我没有得到我所期望的数据,但最后添加的数据行三次。如果这是有意义的,这里是代码。

        ArrayList<ArrayList<String>> module_list = new ArrayList<ArrayList<String>>();
    ArrayList<String> new_row = new ArrayList<String>();
    new_row.add(module_description,"Description0");
    new_row.add(module_address, "Address0");
    new_row.add(module_type, "Type0");
    module_list.add(0,new_row);
    new_row.add(module_description, "Description1");
    new_row.add(module_address,"Address1");
    new_row.add(module_type, "Type1");
    module_list.add(1,new_row);
    new_row.add(module_description, "Description2");
    new_row.add(module_address, "Address2");
    new_row.add(module_type,"Type2");;
    module_list.add(2, new_row);

    for(int row = 0; row<module_list.size() ; row++) {
        ArrayList<String> returned_row = new ArrayList<String>();
        returned_row = module_list.get(row);
        Log.d("APP", "Row: "+ Integer.toString(row));
        Log.d("APP", "Module description: " + returned_row.get(module_description));
        Log.d("APP", "Module address: " + returned_row.get(module_address));
        Log.d("APP", "Module type: " + returned_row.get(module_type));
    }

我本来打算依次打印数组列表中每一行的内容,结果却得到:

Row: 0
Module description: Description2
Module address: Address2
Module type: Type2
Row: 1
Module description: Description2
Module address: Address2
Module type: Type2
Row: 2
Module description: Description2
Module address: Address2
Module type: Type2

这是添加的最后一行,但在所有行的位置。以前有人遇到过这样的问题吗?如有任何建议,我将不胜感激。

在添加到module_list

之前,必须每次初始化new_row
ArrayList<ArrayList<String>> module_list = new ArrayList<ArrayList<String>>();
        ArrayList<String> new_row = new ArrayList<String>();
        new_row.add(module_description,"Description0");
        new_row.add(module_address, "Address0");
        new_row.add(module_type, "Type0");
        module_list.add(0,new_row);
        new_row = new ArrayList<String>();
        new_row.add(module_description, "Description1");
        new_row.add(module_address,"Address1");
        new_row.add(module_type, "Type1");
        module_list.add(1,new_row);
        new_row = new ArrayList<String>();
        new_row.add(module_description, "Description2");
        new_row.add(module_address, "Address2");
        new_row.add(module_type,"Type2");;
        module_list.add(2, new_row);

        for(int row = 0; row<module_list.size() ; row++) {
            ArrayList<String> returned_row = new ArrayList<String>();
            returned_row = module_list.get(row);
            Log.d("APP", "Row: "+ Integer.toString(row));
            Log.d("APP", "Module description: " + returned_row.get(module_description));
            Log.d("APP", "Module address: " + returned_row.get(module_address));
            Log.d("APP", "Module type: " + returned_row.get(module_type));
        }

如果你没有初始化它,那么它将覆盖现有键的值。

试试这个:

HashMap<Integer, ArrayList<String>> module_list =      new HashMap<Integer, ArrayList<String>>();
ArrayList<String> new_row = new ArrayList<String>();
new_row.add(module_description,"Description0");
new_row.add(module_address, "Address0");
new_row.add(module_type, "Type0");
module_list.put(0,new_row);
new_row.add(module_description, "Description1");
new_row.add(module_address,"Address1");
new_row.add(module_type, "Type1");
module_list.put(1,new_row);
new_row.add(module_description, "Description2");
new_row.add(module_address, "Address2");
new_row.add(module_type,"Type2");;
module_list.put(2, new_row);

不用:module_list.add (0, new_row);你能检查一下module_list.add (new_row);因为Arraylist是一个有序集合,所以不需要给出索引

相关内容

  • 没有找到相关文章

最新更新