Java - 配列を印刷する方法

この記事では、Java Arrayを印刷する方法をいくつか紹介します。

1. Arrays.toString

これは配列を印刷する最も簡単な方法です—​Arrays.toString`(JDK 1.5以降)

PrintArray.java

package com.mkyong.utils.print;

import java.util.Arrays;

public class PrintArray {

    public static void main(String[]args) {

       //array
        String[]arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
        System.out.println(Arrays.toString(arrayStr));
       //Output :[Java, Node, Python, Ruby]
        int[]arrayInt = {1, 3, 5, 7, 9};
        System.out.println(Arrays.toString(arrayInt));
       //Output :[1, 3, 5, 7, 9]
       //2d array, need Arrays.deepToString
        String[][]deepArrayStr = new String[][]{{"mkyong1", "mkyong2"}, {"mkyong3", "mkyong4"}};
        System.out.println(Arrays.toString(deepArrayStr));
       //Output :[[Ljava.lang.String;@23fc625e,[Ljava.lang.String;@3f99bd52]
        System.out.println(Arrays.deepToString(deepArrayStr));
       //Output :[[mkyong1, mkyong2],[mkyong3, mkyong4]
        int[][]deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
        System.out.println(Arrays.toString(deepArrayInt));
       //Output :[[[email protected],[[email protected]]
        System.out.println(Arrays.deepToString(deepArrayInt));
       //Output :[[1, 3, 5, 7, 9],[2, 4, 6, 8, 10]]
    }

}

出力

…​.[Java, Node, Python, Ruby][1, 3, 5, 7, 9][[Ljava.lang.String;@23fc625e,[Ljava.lang.String;@3f99bd52] [[[email protected],[[email protected]][[1, 3, 5, 7, 9],[2, 4, 6, 8, 10]]…​.

2. JDK 8ストリーム

JDK 8では、https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html[Stream]に変換して出力できます。

PrintArray.java

package com.mkyong.utils.print;

import java.util.Arrays;

public class PrintArray {

    public static void main(String[]args) {

       //array
        String[]arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
        Arrays.stream(arrayStr).forEach(System.out::println);

        int[]arrayInt = {1, 3, 5, 7, 9};
        Arrays.stream(arrayInt).forEach(System.out::println);

       //2d array
        String[][]deepArrayStr = new String[][]{{"mkyong1", "mkyong2"}, {"mkyong3", "mkyong4"}};
        Arrays.stream(deepArrayStr).flatMap(x -> Arrays.stream(x)).forEach(System.out::println);

        int[][]deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
        Arrays.stream(deepArrayInt).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println);

    }

}

出力

Java
Node
Python
Ruby
1
3
5
7
9
mkyong1
mkyong2
mkyong3
mkyong4
1
3
5
7
9
2
4
6
8
10

3. Json Anything

これは私の好みの方法です。http://wiki.fasterxml.com/JacksonRelease20[Jackson2]ライブラリを使用して、JSON形式の文字列で何かを印刷します。

PrintUtils.java

package com.mkyong.utils.print;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PrintUtils {

    private static final ObjectMapper om = new ObjectMapper();

    public static void print(Object object) {
        String result;
        try {
            result = om.writerWithDefaultPrettyPrinter().writeValueAsString(object);
            System.out.println(result);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

}

PrintArray.java

package com.mkyong.utils.print;

public class PrintArray {

    public static void main(String[]args) {

       //array
        String[]arrayStr = new String[]{"Java", "Node", "Python", "Ruby"};
        PrintUtils.print(arrayStr);

        int[]arrayInt = {1, 3, 5, 7, 9};
        PrintUtils.print(arrayInt);

       //2d array
        String[][]deepArrayStr = new String[][]{{"mkyong1", "mkyong2"}, {"mkyong3", "mkyong4"}};
        PrintUtils.print(deepArrayStr);

        int[][]deepArrayInt = new int[][]{{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
        PrintUtils.print(deepArrayInt);

    }

}

出力

…​.["Java", "Node", "Python", "Ruby"][1, 3, 5, 7, 9][["mkyong1", "mkyong2"],["mkyong3", "mkyong4"]][[1, 3, 5, 7, 9],[2, 4, 6, 8, 10]]…​.

参考文献

JavaDoc]。リンク://java/jackson-2-convert-java-object-to-from-json/[Jackson 2

JavaオブジェクトをJSONからJSONに変換]。 Jackson 2公式サイト