How to add arrays to ArrayList?

前端 未结 3 1945
天涯浪人
天涯浪人 2021-01-24 16:11

I have an int[3][3] array and it contains only 0 or 1 values, if the value is 1 I want to add the coordinates of this value in the ArrayList as int[2] array, but I don\'t know w

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-24 16:29

    You need to create new coordinates array for each i,j pair you want to place in your list. For now you are placing same array multiple times which remembers last set pair.

    In other words you need to

    if (board[i][j] == 1) {
        coordinates = new int[2];//add this line
        coordinates[0] = i;
        coordinates[1] = j;
        arrayList.add(coordinates);
    
    }
    

提交回复
热议问题