3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

配列の中身をSystem.out.printlnで出力するときは…

Last updated at Posted at 2023-08-13

下記のように、配列を単にSystem.out.printlnに渡しても 意味不明な文字列が表示されるだけで配列の中身は表示されません。

String arr1[] = {"AA", "BB", "CC", "DD", "EE"};
int arr2[] = {1,2,3,4,5};
 
System.out.println(arr1);
System.out.println(arr2);
コンソール結果…
[Ljava.lang.String;@15db9742
[I@6d06d69c

解決方法

配列(Array)の中身(要素)を表示するには、「Arraysクラス」の「toStringメソッド」 やdeeptoStringメソッドを使用する。

ArraysクラスのtoStringメソッドは、配列を文字列に変換するメソッドである。

【toStringメソッドの書き方】
「import java.util.Arrays;」を頭に記述。
「Arrays.toString(配列);」

import java.util.Arrays;
 
String arr1[] = {"AA", "BB", "CC", "DD", "EE"};
int arr2[] = {1,2,3,4,5};
 
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
コンソール結果…
[AA, BB, CC, DD, EE]
[1, 2, 3, 4, 5]
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?