什么';打印Java数组的最简单方法是什么?
在Java中,数组不重写什么';打印Java数组的最简单方法是什么?,java,arrays,printing,Java,Arrays,Printing,在Java中,数组不重写toString(),因此如果您试图直接打印一个数组,则会得到className+'@'+数组的十六进制,如Object.toString()所定义: // 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number
toString()
,因此如果您试图直接打印一个数组,则会得到className
+'@'+数组的十六进制,如Object.toString()
所定义:
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
但通常情况下,我们想要的东西更像[1,2,3,4,5]
。最简单的方法是什么?以下是一些输入和输出示例:
// Array of primitives:
int[] intArray = new int[] {1, 2, 3, 4, 5};
//output: [1, 2, 3, 4, 5]
// Array of object references:
String[] strArray = new String[] {"John", "Mary", "Bob"};
//output: [John, Mary, Bob]
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
由于Java5,您可以在数组中使用或来表示数组。请注意,Object[]
version对数组中的每个对象调用.toString()
。输出甚至按照您要求的方式进行装饰
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
示例:
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
- 简单数组:
输出:
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
- 嵌套数组:
输出:
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
数组: 输出:double
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
数组: 输出:int
[John, Mary, Bob]
[[John, Mary], [Alice, Bob]]
[7.0, 9.0, 5.0, 1.0, 3.0 ]
[7, 9, 5, 1, 3 ]
item 1 item 2 item 3
// 1) toString() int[] arrayInt = new int[] {10, 20, 30, 40, 50}; System.out.println(Arrays.toString(arrayInt)); // 2 for loop() for (int number : arrayInt) { System.out.println(number); } // 3 for each() for(int x: arrayInt){ System.out.println(x); }
{1,2,3,4,5} {John,Mary,Bob}
import java.util.Arrays;
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
然后尝试:
System.out.println(Arrays.toString(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
或者,如果您的数组包含其他数组作为元素:
System.out.println(Arrays.deepToString(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
如果您使用的是Java 1.4,则可以执行以下操作:
System.out.println(Arrays.asList(array));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
(当然,这也适用于1.5+版本。)知道这一点很好,但是,至于“总是先检查标准库”,我永远不会偶然发现
数组的诀窍。toString(myarray)
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
--因为我专注于myarray的类型,想看看如何做到这一点。我不想重复这件事:我想要一个简单的调用,使它与我在Eclipse调试器和myarray中看到的类似。toString()只是没有这样做
import java.util.Arrays;
.
.
.
System.out.println( Arrays.toString( myarray ) );
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
Arrays.deepToString(arr)
仅在一行上打印
int[][] table = new int[2][2];
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
要将表格打印为二维表格,我必须执行以下操作:
System.out.println(Arrays.deepToString(table).replaceAll("],", "]," + System.getProperty("line.separator")));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
这看起来像是
数组。deepToString(arr)
方法应该使用分隔符字符串,但不幸的是它没有。在我看来,使用常规for循环是打印数组的最简单方法。
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
这里有一个基于intArray的示例代码
for (int i = 0; i < intArray.length; i++) {
System.out.print(intArray[i] + ", ");
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
for(int i=0;i
它的输出和你的一样
在JDK1.8中,可以使用聚合运算和lambda表达式:
String[] strArray = new String[] {"John", "Mary", "Bob"};
// #1
Arrays.asList(strArray).stream().forEach(s -> System.out.println(s));
// #2
Stream.of(strArray).forEach(System.out::println);
// #3
Arrays.stream(strArray).forEach(System.out::println);
/* output:
John
Mary
Bob
*/
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
String[] strArray = new String[] {"John", "Mary", "Bob"};
// #1
Arrays.asList(strArray).stream().forEach(s -> System.out.println(s));
// #2
Stream.of(strArray).forEach(System.out::println);
// #3
Arrays.stream(strArray).forEach(System.out::println);
/* output:
John
Mary
Bob
*/
要添加到所有答案中,还可以选择将对象打印为JSON字符串
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
使用Jackson:
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
System.out.println(ow.writeValueAsString(anyArray));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
使用Gson:
Gson gson = new Gson();
System.out.println(gson.toJson(anyArray));
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
如果数组是char[]类型,还有一种方法:
char A[] = {'a', 'b', 'c'};
System.out.println(A); // no other arguments
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
印刷品
abc
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
公共类打印机{
公共静态void main(字符串[]args){
字符串a[]=新字符串[4];
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入数据”);
对于(int i=0;i<4;i++){
a[i]=sc.nextLine();
}
System.out.println(“输入的数据为”);
用于(字符串i:a){
系统输出打印LN(i);
}
}
}
我尝试过的简化快捷方式是:
int x[] = {1,2,3};
String printableText = Arrays.toString(x).replaceAll("[\\[\\]]", "").replaceAll(", ", "\n");
System.out.println(printableText);
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
它会打印出来
1
2
3
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
这种方法不需要循环,最好只用于Java 8之前的小型阵列
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
我们可以使用array.toString(array)
打印一维数组,而array.deepToString(array)
打印多维数组
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
爪哇8
现在我们有了Stream
和lambda
选项来打印数组
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
打印一维数组:
public static void main(String[] args) {
int[] intArray = new int[] {1, 2, 3, 4, 5};
String[] strArray = new String[] {"John", "Mary", "Bob"};
//Prior to Java 8
System.out.println(Arrays.toString(intArray));
System.out.println(Arrays.toString(strArray));
// In Java 8 we have lambda expressions
Arrays.stream(intArray).forEach(System.out::println);
Arrays.stream(strArray).forEach(System.out::println);
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
输出为:
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
[1,2,3,4,5][约翰、玛丽、鲍勃]
1
2
3
4
5
约翰
玛丽
鲍勃
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
打印多维数组
为了防止打印多维数组,我们可以使用数组。deepToString(数组)
作为:
public static void main(String[] args) {
int[][] int2DArray = new int[][] { {11, 12}, { 21, 22}, {31, 32, 33} };
String[][] str2DArray = new String[][]{ {"John", "Bravo"} , {"Mary", "Lee"}, {"Bob", "Johnson"} };
//Prior to Java 8
System.out.println(Arrays.deepToString(int2DArray));
System.out.println(Arrays.deepToString(str2DArray));
// In Java 8 we have lambda expressions
Arrays.stream(int2DArray).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println);
Arrays.stream(str2DArray).flatMap(x -> Arrays.stream(x)).forEach(System.out::println);
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
现在需要注意的是,方法Arrays.stream(T[])
,在int[]
的情况下,它返回我们stream
,然后方法flatMapToInt()
将流的每个元素与通过对每个元素应用提供的映射函数生成的映射流的内容进行映射
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
输出为:
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
[11,12]、[21,22]、[31,32,33][[John,Bravo],[Mary,Lee],[Bob,Johnson]
11
12
21
22
31
32
33
约翰
好极了
玛丽
李
鲍勃
约翰逊
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
可以选择使用org.apache.commons.lang3.StringUtils.join(*)方法
例如:
String[] strArray = new String[] { "John", "Mary", "Bob" };
String arrayAsCSV = StringUtils.join(strArray, " , ");
System.out.printf("[%s]", arrayAsCSV);
//output: [John , Mary , Bob]
String[] items = {"item 1", "item 2", "item 3"};
for(int i = 0; i < items.length; i++) {
System.out.println(items[i]);
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
我使用了以下依赖项
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
org.apache.commons
commons-lang3
3.3.2
从Java 8开始,还可以利用提供的join()
方法打印出数组元素,不带括号,并由选择的分隔符分隔(以下示例中的空格字符):
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
输出将是“你好,朋友!”。Arrays.toString
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
作为一个直接的回答,使用和方法,是最好的
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
Java8-Stream.collect(joining()),Stream.forEach
下面我尝试列出一些建议的其他方法,试图稍加改进,最值得注意的是使用操作符,使用收集器
,来模拟字符串.join
的作用
int[] ints = new int[] {1, 2, 3, 4, 5};
System.out.println(IntStream.of(ints).mapToObj(Integer::toString).collect(Collectors.joining(", ")));
System.out.println(IntStream.of(ints).boxed().map(Object::toString).collect(Collectors.joining(", ")));
System.out.println(Arrays.toString(ints));
String[] strs = new String[] {"John", "Mary", "Bob"};
System.out.println(Stream.of(strs).collect(Collectors.joining(", ")));
System.out.println(String.join(", ", strs));
System.out.println(Arrays.toString(strs));
DayOfWeek [] days = { FRIDAY, MONDAY, TUESDAY };
System.out.println(Stream.of(days).map(Object::toString).collect(Collectors.joining(", ")));
System.out.println(Arrays.toString(days));
// These options are not the same as each item is printed on a new line:
IntStream.of(ints).forEach(System.out::println);
Stream.of(strs).forEach(System.out::println);
Stream.of(days).forEach(System.out::println);
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
我最近在一家报纸上看到了这篇文章。编写
Arrays.toString(arr)不是很方便
,然后导入java.util.array代码>所有时间
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
请注意,这无论如何都不是永久性修复。这只是一种可以简化调试的技巧。
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
直接打印数组会给出内部表示和哈希代码。现在,所有类都将Object
作为父类型。那么,为什么不破解对象。toString()
?未经修改,对象类如下所示:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
如果更改为:
public String toString() {
if (this instanceof boolean[])
return Arrays.toString((boolean[]) this);
if (this instanceof byte[])
return Arrays.toString((byte[]) this);
if (this instanceof short[])
return Arrays.toString((short[]) this);
if (this instanceof char[])
return Arrays.toString((char[]) this);
if (this instanceof int[])
return Arrays.toString((int[]) this);
if (this instanceof long[])
return Arrays.toString((long[]) this);
if (this instanceof float[])
return Arrays.toString((float[]) this);
if (this instanceof double[])
return Arrays.toString((double[]) this);
if (this instanceof Object[])
return Arrays.deepToString((Object[]) this);
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
通过在命令行中添加以下内容,可以简单地将这个修改过的类添加到类路径中:-Xbootclasspath/p:target/classes
// 1) toString()
int[] arrayInt = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arrayInt));
// 2 for loop()
for (int number : arrayInt) {
System.out.println(number);
}
// 3 for each()
for(int x: arrayInt){
System.out.println(x);
}
现在,由于Java 5提供了deepToString(..)
,可以很容易地将toString(..)
更改为deepToStr