如何在android中创建一个arraylist数组

时间:2016-02-27 16:39:13

标签: android arrays arraylist

    ArrayList<String> parsedPODAY_ID=new ArrayList<>();
    ArrayList<String> parsedPODAY_P_IMG=new ArrayList<>();
    ArrayList<String> parsedPODAY_NAME=new ArrayList<>();
    ArrayList<String> parsedPODAY_PRICE=new ArrayList<>();
    ArrayList<String> parsedPODAY_OFFPRICE=new ArrayList<>();

我创建了arraylist。但是我怎样才能创建这些arraylist的arraylist或数组。 在此先感谢!!

2 个答案:

答案 0 :(得分:1)

只需使用

  ArrayList<ArrayList<String>> parent = new ArrayList<>();

添加喜欢

parent.add(parsedPODAY_OFFPRICE);

答案 1 :(得分:0)

尝试以下

ArrayList<ArrayList<String>> mainArrayList = new ArrayList<ArrayList<String>>();

ArrayList<String> subOneArray = new ArrayList<String>();
subOneArray.Add("first");
subOneArray.Add("second");

ArrayList<String> subTwoArray = new ArrayList<String>();
subTwoArray.Add("first");

mainArrayList.Add(subOneArray);    
mainArrayList.Add(subTwoArray);
上面的代码在更改时会有点复杂,另一种方式是

 // create map to store
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        // create list one and store values
        List<String> valSetOne = new ArrayList<String>();
        valSetOne.add("Apple");
        valSetOne.add("Aeroplane");
        // create list two and store values
        List<String> valSetTwo = new ArrayList<String>();
        valSetTwo.add("Bat");
        valSetTwo.add("Banana");
        // create list three and store values
        List<String> valSetThree = new ArrayList<String>();
        valSetThree.add("Cat");
        valSetThree.add("Car");
        // put values into map
        map.put("A", valSetOne);
        map.put("B", valSetTwo);
        map.put("C", valSetThree);
        // iterate and display values
        System.out.println("Fetching Keys and corresponding [Multiple] Values n");
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            String key = entry.getKey();
            List<String> values = entry.getValue();
            System.out.println("Key = " + key);
            System.out.println("Values = " + values + "n");
        }