Java –如何打印数组

Java –如何打印数组在本文中,我们将向您展示几种打印Java数组的方法。1.Arrays.toString这是打印数组的最简单方法–Arrays.toString(自JDK1.5起)PrintArray.javapackagecom.mkyong.utils.print;importjava.util.Arrays;publicclassPrintArray{…

大家好,欢迎来到IT知识分享网。Java

在本文中,我们将向您展示几种打印Java数组的方法。

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 : [[I@3a71f4dd, [I@7adf9f5f]

        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]
[[mkyong1, mkyong2], [mkyong3, mkyong4]]
[[I@3a71f4dd, [I@7adf9f5f]
[[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]

2. JDK 8流

在JDK 8中,我们可以将其转换为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.杰森

这是我最喜欢的方法,请使用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 ] ]

参考文献

  1. Steam Javasoc
  2. 数组JavaDoc
  3. 杰克逊2将Java对象转换为JSON
  4. 杰克逊2官方网站

翻译自: https://mkyong.com/java/java-how-to-print-an-array/

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/11573.html

(0)
上一篇 2024-03-21 20:15
下一篇 2024-03-22 07:15

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信