๐๋ฌธ์
2์ฐจ์ ๋ฐฐ์ด์ด ์ฃผ์ด์ก์ ๋ ํ์ ์ํํ ์ถ๊ฐ ๋ฐ ์ ๊ฑฐ๋ฅผ ์ํด `List`๋ก ๋ณํํ์ฌ ์ฌ์ฉํ๊ณ ์ ํ๋ค.
๐ํด๊ฒฐ
1. ๋ฐฐ์ด์ ์ ๋ค๋ฆญ ํ์ ์ผ๋ก ๊ฐ์ง `List`๋ฅผ ๋ง๋ ๋ค.
2. `List<T>`๋ฅผ ์ ๋ค๋ฆญ ํ์ ์ผ๋ก ๊ฐ์ง `List`๋ฅผ ๋ง๋ ๋ค.
๐๊ตฌํ
1. ๋ฐฐ์ด์ ์ ๋ค๋ฆญ ํ์ ์ผ๋ก ๊ฐ์ง `List`๋ฅผ ๋ง๋ ๋ค.
int[][] intArr = new int[][]{
{0, 0},
{0, 1},
{0, 2}
};
List<int[]> list = new ArrayList<>(Arrays.asList(intArr));
for (int[] ints : list) {
System.out.println(Arrays.toString(ints));
}
// [0, 0]
// [0, 1]
// [0, 2]
2. `List<Object>`๋ฅผ ์ ๋ค๋ฆญ ํ์
์ผ๋ก ๊ฐ์ง `List`๋ฅผ ๋ง๋ ๋ค.
int[][] intArr = new int[][]{
{0, 0},
{0, 1},
{0, 2}
};
List<List<Integer>> list = new ArrayList<>();
for (int[] ints : intArr) {
List<Integer> innerList = new ArrayList<>();
for (int anInt : ints) {
innerList.add(anInt);
}
list.add(innerList);
}
System.out.println(list);
// [[0, 0], [0, 1], [0, 2]]
๐2์ฐจ์ ๋ฐฐ์ด์ List๋ก ๋ณํํ๊ธฐ
int[][] answer = list.toArray(int[][]::new);
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ฌธ์(char)์ ๋ฌธ์์ด(String)์ ๋ค๋ฃจ๋ ํจ์ (0) | 2024.05.30 |
---|---|
[JAVA] ๊ธฐ๋ณธ ์ ๋ ฌ ๋์ ์ํ๋ ๋ฐฉ์์ผ๋ก ์ ๋ ฌํ๊ธฐ (0) | 2023.12.12 |