では、配列と List の複雑ネストをまとめた ASCII 図+出力例付きの一覧を作ります。これで「多次元・ネスト構造での println / toString / deepToString の挙動」が一目でわかるようになります。
1. ネスト構造一覧
| 構造 | 変数例 | println | toString | deepToString / 個別表示 |
|---|---|---|---|---|
| 1次元配列 | int[] one = {1,2,3} | [I@xxxxxx | [1,2,3] | [1,2,3] |
| 2次元配列 | int[][] two = {{10,20},{30,40}} | [I@xxxx, [I@yyyy] | [ [I@xxxx, [I@yyyy] ] | [[10,20],[30,40]] |
| null 配列 | int[] a = null | null | NPE | [null] (Object 配列でラップ) |
| 配列の一部表示 | int[] big → Arrays.copyOfRange(big,0,5) | – | [1,2,3,4,5] | [1,2,3,4,5] |
| 配列の中に List | Object[] arrayWithList = {Arrays.asList(1,2), Arrays.asList(3,4)} | [Ljava.lang.Object;@xxxx | [[1,2],[3,4]] | [[1,2],[3,4]] |
| List の中に配列 | List<Object> listWithArray = Arrays.asList(new int[]{5,6}, new int[]{7,8}) | [[I@xxxx, [I@yyyy]] | – | 個別に Arrays.toString → [5,6] [7,8] |
2. ASCII 図でイメージ化
1次元配列
one: [1,2,3]
^ ^ ^
val val val
println → REF
toString → [1,2,3]
deepToString → [1,2,3]
2次元配列
two: [ row0 , row1 ]
| |
v v
row0: [10,20]
row1: [30,40]
println → REF
toString → [REF_row0, REF_row1]
deepToString → [[10,20],[30,40]]
配列の中に List
arrayWithList: [ list0 , list1 ]
| |
v v
list0: [1,2]
list1: [3,4]
println → REF
toString → [[1,2],[3,4]]
deepToString → [[1,2],[3,4]]
List の中に配列
listWithArray: [ arr0 , arr1 ]
| |
v v
arr0: [5,6]
arr1: [7,8]
println → [[I@xxxx, [I@yyyy]]
個別に toString → [5,6], [7,8]
null 配列
a: null
println → null
toString → NPE
deepToString → [null]
部分表示
big: [1,2,3,4,5,6,7,8,9,10]
firstN = copyOfRange(big,0,5)
Arrays.toString(firstN) → [1,2,3,4,5]
3. コピペで確認コード
import java.util.Arrays;
import java.util.List;
public class FullNestedDemo {
public static void main(String[] args) {
int[] one = {1,2,3};
int[][] two = {{10,20},{30,40}};
int[] nullArray = null;
int[] big = {1,2,3,4,5,6,7,8,9,10};
Object[] arrayWithList = { Arrays.asList(1,2), Arrays.asList(3,4) };
List<Object> listWithArray = Arrays.asList(new int[]{5,6}, new int[]{7,8});
System.out.println("1次元配列 println: " + one);
System.out.println("1次元配列 toString: " + Arrays.toString(one));
System.out.println("1次元配列 deepToString: " + Arrays.deepToString(new Object[]{one}));
System.out.println();
System.out.println("2次元配列 println: " + two);
System.out.println("2次元配列 toString: " + Arrays.toString(two));
System.out.println("2次元配列 deepToString: " + Arrays.deepToString(two));
System.out.println();
System.out.println("null 配列 println: " + nullArray);
System.out.println("null 配列 deepToString: " + Arrays.deepToString(new Object[]{nullArray}));
System.out.println();
int[] firstN = Arrays.copyOfRange(big, 0, 5);
System.out.println("部分表示: " + Arrays.toString(firstN));
System.out.println();
System.out.println("配列の中に List:");
System.out.println("println: " + arrayWithList);
System.out.println("toString: " + Arrays.toString(arrayWithList));
System.out.println("deepToString: " + Arrays.deepToString(arrayWithList));
System.out.println();
System.out.println("List の中に配列:");
System.out.println("println: " + listWithArray);
System.out.println("個別に toStringで中身表示:");
listWithArray.stream()
.map(o -> Arrays.toString((int[])o))
.forEach(System.out::println);
}
}
Java💡 ポイント総まとめ
- 配列は println だけでは参照表示になりやすい
Arrays.toString→ 1次元配列は安全に中身表示Arrays.deepToString→ 多次元・配列の中に List も再帰展開可能- List の中に配列は println だけでは中身が見えない → 個別に
Arrays.toString - null や部分表示は Object 配列ラップや
copyOfRangeを活用
