访问ArrayList的多维数组中的元素

时间:2015-06-05 04:51:17

标签: java arraylist

public static void main(String[] args) {
    new T1();
    ArrayList<Integer>[][] b = new ArrayList[2][3];

    b[1][2] = new ArrayList();
    b[1][2].add(1);

    //print [1]
    System.out.println(b[1][2]);
}

我做了2D array of ArrayList。我们如何使用ArrayList语句检查是否在if的特定索引处添加了数字?

if (b[1][2] == 1) // <---
    System.out.println("number 1");

2 个答案:

答案 0 :(得分:0)

 ArrayList<Integer>[][] b = new ArrayList[2][3];

 b[1][2] = new ArrayList();
 b[1][2].add(1);

 // print [1]
 System.out.println( b[1][2].get(0));

您需要使用get()的{​​{1}}方法。

答案 1 :(得分:0)

您有3 Dimensional个数据结构。我不知道what are you trying to achieve

if条件中的问题是你将arraylist(b [1] [2]包含ArrayList)与整数1进行比较。

您可以使用以下代码

if (b[1][2].get(0) == 1) // checking if 1 is present at first location in the arraylist present at [1,2] location in b array.
    System.out.println("number 1");

if (b[1][2].contains(1)) // checking if 1 is present at any location in arraylist present at [1,2] location in b array.
    System.out.println("number 1");