打印Java数组的最简单方法是什么?
在Java中,数组不重写toString,因此如果您尝试直接打印一个,则会得到数组的类名+'@'+十六进制,如Object.toString所定义:打印Java数组的最简单方法是什么?,java,arrays,printing,Java,Arrays,Printing,在Java中,数组不重写toString,因此如果您尝试直接打印一个,则会得到数组的类名+'@'+十六进制,如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)
// 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,您可以在数组中使用或来表示数组。请注意,对象[]版本在数组中的每个对象上调用.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}
双阵列:
输出:
[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}
始终首先检查标准库
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+版本。很高兴知道这一点,但是,对于总是先检查标准库,我永远不会偶然发现数组的诀窍
// 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.DeepToStringar仅在一行上打印
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);
}
看起来Arrays.deeptostringar方法应该使用分隔符字符串,但不幸的是它没有。在我看来,使用正则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);
}
它的输出和你的一样
在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);
}
我尝试过的简化快捷方式是:
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);
}
我们可以使用Arrays.toStringarray打印一维数组,使用Arrays.deepToStringarray打印多维数组
// 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);
}
打印一维数组:
// 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);
}
打印多维阵列
为了防止打印多维数组,我们可以将Arrays.deepToStringarray用作:
// 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.streamT[],在int[]的情况下,它返回us Stream,然后方法flatMapToInt将Stream的每个元素与通过对每个元素应用提供的映射函数生成的映射流的内容进行映射
// 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]]
[约翰,好极了],[玛丽,李],[鲍勃,约翰逊]]
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);
}
从Java 8开始,还可以利用提供的join方法打印出数组元素,不带括号,并由选择的分隔符分隔,该分隔符是下面所示示例的空格字符:
String[] greeting = {"Hey", "there", "amigo!"};
String delimiter = " ";
String.join(delimiter, greeting)
// 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);
}
输出将在那里嘿,朋友 数组.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.collectjoining,Stream.forEach
下面我尝试列出一些建议的其他方法,试图稍加改进,最值得注意的是使用操作符,使用收集器,来模拟String.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);
}
我最近在一家报纸上看到了这篇文章。写数组不是很方便。tostringar;,然后导入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);
}
现在,随着deepToString的可用性。。自Java5以来,toString。。可以轻松更改为deepToString。。添加对包含其他阵列的阵列的支持
// 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);
}
我发现这是一个非常有用的hack,如果Java能够简单地添加它,那就太好了。我理解使用非常大的数组的潜在问题,因为字符串表示
离子可能有问题。可能会通过类似System.Out或PrintWriter之类的程序来处理这种情况 无论您使用哪个JDK版本,它都应该始终有效:
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) 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);
}
例如:
int[] a = new int[]{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);
}
替换为:
Integer[] a = new Integer[]{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);
}
更新:
// 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) 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 void printArray(int [] a){
//write printing code
}
// 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中,这很容易。有两个关键词
// 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.streamintArray.forEach
方法引用:::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);
}
如果要在同一行中打印数组中的所有元素,则只需使用print而不是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);
}
另一种没有方法参考的方法只需使用:
int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(intArray));
// 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" };
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);
}
在Java中打印数组的不同方法:
// 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);
}
简单方法
List<String> list = new ArrayList<String>();
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
// Print the list in console
System.out.println(list);
// 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);
}
打印阵列的阵列
String[] arr1 = new String[] { "Fifth", "Sixth" };
String[] arr2 = new String[] { "Seventh", "Eight" };
String[][] arrayOfArray = new String[][] { arr1, arr2 };
System.out.println(arrayOfArray);
System.out.println(Arrays.toString(arrayOfArray));
System.out.println(Arrays.deepToString(arrayOfArray));
// 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);
}
输出:[[Ljava.lang.String;@1ad086a[[Ljava.lang.String;@10385c1,
[Ljava.lang.String;@42719c][[Fifth,Sixth],[Seven,Otherh]]
// 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);
}
资源:每个循环也可用于打印数组元素:
int array[] = {1, 2, 3, 4, 5};
for (int i:array)
System.out.println(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);
}
打印数组有以下几种方法
// 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);
}
Arrays.stream(myArray).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);
}
如果包含ISO-8859-1字符,则可以将其打印为字符串
// 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 s = new String(bytes, StandardChars.ISO_8559);
System.out.println(s);
// to reverse
byte[] bytes2 = s.getBytes(StandardChars.ISO_8559);
或者如果它包含UTF-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);
}
String s = new String(bytes, StandardChars.UTF_8);
System.out.println(s);
// to reverse
byte[] bytes2 = s.getBytes(StandardChars.UTF_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);
}
String s = DatatypeConverter.printHexBinary(bytes);
System.out.println(s);
// to reverse
byte[] bytes2 = DatatypeConverter.parseHexBinary(s);
或者,如果您想将其打印为base64
// 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 s = DatatypeConverter.printBase64Binary(bytes);
System.out.println(s);
// to reverse
byte[] bytes2 = DatatypeConverter.parseBase64Binary(s);
或者如果要打印有符号字节值的数组
// 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 s = Arrays.toString(bytes);
System.out.println(s);
// to reverse
String[] split = s.substring(1, s.length() - 1).split(", ");
byte[] bytes2 = new byte[split.length];
for (int i = 0; i < bytes2.length; i++)
bytes2[i] = Byte.parseByte(split[i]);
String s = Arrays.toString(
IntStream.range(0, bytes.length).map(i -> bytes[i] & 0xFF).toArray());
System.out.println(s);
// to reverse
String[] split = s.substring(1, s.length() - 1).split(", ");
byte[] bytes2 = new byte[split.length];
for (int i = 0; i < bytes2.length; i++)
bytes2[i] = (byte) Integer.parseInt(split[i]); // might need a range check.
或者,如果要打印无符号字节值数组
// 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 s = Arrays.toString(bytes);
System.out.println(s);
// to reverse
String[] split = s.substring(1, s.length() - 1).split(", ");
byte[] bytes2 = new byte[split.length];
for (int i = 0; i < bytes2.length; i++)
bytes2[i] = Byte.parseByte(split[i]);
String s = Arrays.toString(
IntStream.range(0, bytes.length).map(i -> bytes[i] & 0xFF).toArray());
System.out.println(s);
// to reverse
String[] split = s.substring(1, s.length() - 1).split(", ");
byte[] bytes2 = new byte[split.length];
for (int i = 0; i < bytes2.length; i++)
bytes2[i] = (byte) Integer.parseInt(split[i]); // might need a range check.
如果您正在运行jdk 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);
}
public static void print(int[] array) {
StringJoiner joiner = new StringJoiner(",", "[", "]");
Arrays.stream(array).forEach(element -> joiner.add(element + ""));
System.out.println(joiner.toString());
}
int[] array = new int[]{7, 3, 5, 1, 3};
print(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);
}
[7,3,5,1,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) 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}
在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
*/
此外,从Java8开始,还可以利用String类提供的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);
}
string[] greeting = {"Hey", "there", "amigo!"};
String delimiter = " ";
String.join(delimiter, greeting)
`
输出将显示在Hey there amigo!这里有一个可能的打印功能:
// 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 printArray (int [] array){
System.out.print("{ ");
for (int i = 0; i < array.length; i++){
System.out.print("[" + array[i] + "] ");
}
System.out.print("}");
}
输出将为{[1][2][3][4]}对于字符串以外的对象,您希望其表示形式是什么?调用toString的结果?是否使用引号?是的,对象将由其toString方法表示,不使用引号,只是编辑了示例输出。实际上,与奇怪输出密切相关的不一定是内存位置。而是十六进制的哈希代码adecimal.See。要在java8中打印一维或多维数组,请检查。不幸的是,这仅适用于对象数组,而不适用于原语数组。可以使用System.getPropertyline.separator;而不是\r\n,因此它也适用于非Windows。现在可以使用System.lineSeparator打印1、2、3、4、5,作为输出,它打印com最后一个元素后面也是ma。最后一个元素后面没有逗号的解决方案是什么?可以用System.out.printintArray[i]替换循环中的代码;ifi!=intArray.length-1 System.out.print,;您也可以使用System.out.printi+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);
}
.阵列;如果我们有一个字符串数组,并且想要简单的输出,该怎么办;比如:String[]array={John,Mahta,Sara},我们希望此输出不带括号和逗号:John Mahta Sara?@Hengameh只需将Arrays.toStringarray存储到字符串变量,然后通过的replace方法删除大括号Java@Hengameh现在使用Java8:String.join、Arrays.asListarray。仅仅为了打印目的将数组转换为列表似乎不是一个非常明智的决定;考虑到同一个类有一个toString..,我不明白为什么会有人这样做。这将在单独的一行上打印数组的每个元素,因此它不符合要求。如果要在同一行中打印数组中的所有元素,那么只需使用print而不是println,即int[]intArray=new int[]{1,2,3,4,5};Arrays.streamintArray.forEachSystem.out::print;没有methodreference的antotherway只需使用int[]intArray=newint[]{1,2,3,4,5};System.out.printlnarries.tostringinarray;您想在每个对象上执行这些if条件吗?+1只是为了实现这个想法,但是根据前面的注释,我们是否可以直接修改数组实现,而不是依赖于对象的公共父对象?我们能进一步研究这个想法吗?我并不认为修改java.lang.*对象的默认行为是我鼓励的…@firefhil System.out.printlna[I];与普通for循环一起使用,其中创建索引i并打印每个索引处的值。我已经为每个循环使用了。试试看,希望你能明白我的意思。这比公认的答案更好,因为它能更好地控制分隔符、前缀和后缀。但是,我会在最终的System.out.println中删除多余的toString,并在joiner.addelement.toString中使用它,而不是添加空字符串。此解决方案也同样适用于非基本类型的数组。我的坏元素joiner.add中的element.toString仅适用于非基本类型。对于基元类型,仍然需要使用Integer.toString-like构造。就我个人而言,我对int元素使用了foreach循环:array joiner.addInteger.toString元素;而不是溪流,但这是品味的问题。这就是我所做的。有了它,您可以打印任意复杂的结构,只要它们可以编码为JSON。我总是确保用漂亮的。你的第二个例子是这样做的吗?我认为你需要一个很好的选择来实现这个目标。添加一个依赖项是一个令人尖叫的反模式。commons-lang3是一个非常重要的依赖项,请注意,这个答案是,如果大多数人没有使用java 8,那么最好的解决方案当然也在commons中提到,lang非常流行,应该使用它,而不是自己实现。此类实用方法必须经过测试,并且也应可用于其他项目。只要原语数组不能简单处理,像common lang这样的库和重载方法对我来说就是解决这个问题的最好和有效的方法。。你能公开你的观点让每个人都能学习吗?这个问题问的是java中最简单的方法,所以它是一个单行程序,java当前版本>14,我的解决方案至少是java 8,所以它是适用的。