在Java中,数组不会重写toString(),因此如果您尝试直接打印一个,则会得到数组的className+“@”+hashCode的十六进制,如Object.toString()所定义:

int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(intArray); // Prints something like '[I@3343c8b3'

但通常情况下,我们会想要更像[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]

当前回答

在java8中,这很简单。有两个关键字

stream:数组.stream(intArray).forEach方法引用::printlnint[]intArray=新int[]{1,2,3,4,5};Arrays.stream(intArray).forEach(System.out::println);

如果要在同一行中打印数组中的所有元素,那么只需使用print而不是println,即。

int[] intArray = new int[] {1, 2, 3, 4, 5};
Arrays.stream(intArray).forEach(System.out::print);

另一种没有方法引用的方法只需使用:

int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(intArray));

其他回答

阵列.to字符串

作为一个直接的答案,包括@Esko在内的几个公司使用Arrays.toString和Arrays.deepToString方法提供的解决方案是最好的。

Java 8-Stream.collect(joining()),Stream.forEach

下面,我尝试列出一些建议的其他方法,试图进行一些改进,其中最值得注意的是使用Stream.collect运算符,使用一个加入的收集器来模拟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);

如果您正在运行jdk 8。

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);

输出:

[7,3,5,1,3]

您可以循环遍历数组,在循环时打印出每个项。例如:

String[] items = {"item 1", "item 2", "item 3"};

for(int i = 0; i < items.length; i++) {

    System.out.println(items[i]);

}

输出:

item 1
item 2
item 3

在java8中,这很简单。有两个关键字

stream:数组.stream(intArray).forEach方法引用::printlnint[]intArray=新int[]{1,2,3,4,5};Arrays.stream(intArray).forEach(System.out::println);

如果要在同一行中打印数组中的所有元素,那么只需使用print而不是println,即。

int[] intArray = new int[] {1, 2, 3, 4, 5};
Arrays.stream(intArray).forEach(System.out::print);

另一种没有方法引用的方法只需使用:

int[] intArray = new int[] {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(intArray));

这是一种非常简单的方法来打印数组,而不使用JAVA中的任何循环。->对于,单个或简单阵列:int[]数组=新int[]{1,2,3,4,5,6};System.out.println(数组.toString(数组));输出:[1, 2, 3, 4, 5, 6]->因此,这个2D数组不能用Arrays.toString()打印int[][]array=newint[][][]{{1,2,3,4,5,6,7},{8,9,10,11,12,13,14}};System.out.println(数组.depToString(数组));输出:[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14]]