【发布时间】:2019-08-14 17:52:29
【问题描述】:
在我的程序中,我尝试使用流打印排序的 int 数组。但是我在使用普通流时得到了错误的输出。并且在使用 int 流时会打印出正确的详细信息。
请参阅下面的核心 sn-p 了解更多详细信息。
package com.test.sort.bubblesort;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class BubbleSortWithRecursion {
public static void bubbleSort(int[] arr, int n) {
if (n < 2) {
return;
}
int prevValue;
int nextValue;
for (int index = 0; index < n-1; index++) {
prevValue = arr[index];
nextValue = arr[index+1];
if (prevValue > nextValue) {
arr[index] = nextValue;
arr[index+1] = prevValue;
}
}
bubbleSort(arr, n-1);
}
public static void main(String[] args) {
int arr[] = new int[] {10,1,56,8,78,0,12};
bubbleSort(arr, arr.length);
**//False Output** : [I@776ec8df
String output = Arrays.asList(arr)
.stream()
.map(x -> String.valueOf(x))
.collect(Collectors.joining(","));
System.out.println(output);
//Correct Output : 0,1,8,10,12,56,78
String output2 = IntStream
.of(arr)
.boxed()
.map(x -> Integer.toString(x))
.collect(Collectors.joining(","));
System.out.println(output2);
}
}
我在控制台上得到以下输出:
[I@776ec8df
0,1,8,10,12,56,78
第一行输出是使用正常的java流生成的,这是不正确的。
为什么我使用普通的 JAVA 流会收到虚假内容?我在这里错过了什么吗?
【问题讨论】:
标签: java arrays java-8 java-stream