大家好,又见面了,我是你们的朋友全栈君。
在本文中,我们将向您展示几种打印Java数组的方法。
1. Arrays.toString
这是打印数组的最简单方法– Arrays.toString
(自JDK 1.5起)
PrintArray.java
代码语言:javascript复制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]]
}
}
输出量
代码语言:javascript复制[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
代码语言:javascript复制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);
}
}
输出量
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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);
}
}
输出量
代码语言:javascript复制[ "Java", "Node", "Python", "Ruby" ]
[ 1, 3, 5, 7, 9 ]
[ [ "mkyong1", "mkyong2" ], [ "mkyong3", "mkyong4" ] ]
[ [ 1, 3, 5, 7, 9 ], [ 2, 4, 6, 8, 10 ] ]
参考文献
- Steam Javasoc
- 数组JavaDoc
- 杰克逊2将Java对象转换为JSON
- 杰克逊2官方网站
标签: 数组 Java
翻译自: https://mkyong.com/java/java-how-to-print-an-array/
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/140665.html原文链接:https://javaforall.cn