Java如何打印数组变量中的信息呢?
下文笔者讲述java代码打印数组变量中内容的方法及示例分享
借助apache.commons.lang3 包中的
ArrayUtils方法 即可实现打印数组中信息的效果
注意事项:
maven引入commons jar包可采用以下坐标
<!-- https://search.maven.org/remotecontent?filepath=org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
例:java打印数组中内容的示例
package com.java265.example.commons.lang;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayUtilsToString {
public static void main(String[] args) {
// 将int数组打印为字符串。
int[] numbers = {1, 2, 3, 5, 8, 13, 21, 34};
System.out.println("Numbers = " + ArrayUtils.toString(numbers));
// 将字符串数组打印为字符串。
String[] grades = {"A", "B", "C", "D", "E", "F"};
System.out.println("Grades = " + ArrayUtils.toString(grades));
// 将多维数组打印为字符串。
int[][] matrix = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
System.out.println("Matrix = " + ArrayUtils.toString(matrix));
// 当数组为null时,返回“ Empty”。"Empty" when the array is null.
String[] colors = null;
System.out.println("Colors = " + ArrayUtils.toString(colors, "None"));
}
}
-----运行以上代码,将输出以下信息----
Numbers = {1,2,3,5,8,13,21,34}
Grades = {A,B,C,D,E,F}
Matrix = {{0,1,2},{1,2,3},{2,3,4}}
Colors = None
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


