首页 > 解决方案 > How do I add arrays to an Arraylist of Arrays (Arraylist)

问题描述

I want to save the steps pf my player-character in a Sokoban-game. So at first i want to fill an Array of int x and y, called "pos" with the actual position of the character. And than i want to add this array to an ArrayList of Arrays, called "moves".

Array for one player-position:

int[] pos = new int [2];

ArrayList for all steps, the player made in the level:

Arraylist<Integer[]> moves = new ArrayList<>();

"int[]" makes an error, if placed inside the pointy brackets at the ArrayList.

How do i add the Array pos to the ArrayList moves?

标签: javaarraysarraylist

解决方案


这完全适用于int[]。但是int[] != Integer[],在两个地方都使用相同的。

int[] pos = new int[2];
ArrayList<int[]> arrayList = new ArrayList<>();
pos[0] = 1;
pos[1] = 2;
arrayList.add(pos);
System.out.println(arrayList.get(0)[0]+ " "+ arrayList.get(0)[1]);

推荐阅读