【发布时间】:2020-04-19 09:59:27
【问题描述】:
输入:
1
7
4 2 4 1 4 3 4
输出:
[I@59e84876
4
数组的预期输出应该是 [4 2 4 1 4 3 4] 但为什么它给出了一些随机字符串作为输出,而 Hashset 却给出了完美的输出。
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n=s.nextInt();
for(int i=0;i<n;i++){
int no=s.nextInt();
HashSet<Integer> h = new HashSet<>();
int[] arr = new int[no];
for(int j=0;j<arr.length;j++){
int x=s.nextInt();
arr[j]=x;
h.add(x);
}
}
System.out.println(arr);
System.out.println(h);
}
}
【问题讨论】:
-
你的代码无效,无法编译,h和arr定义了inside的for 但是你将它们打印在 outside for
-
在循环内移动 2 打印时,结果不是您给出的结果,“4”被“[1,2,3,4]”替换。
-
"it give some random string" no,表示它是int(I)及其地址的数组([)
-
@SadmanSakib ,谢谢它正在工作,但为什么要转换成字符串?
-
@bruno,谢谢你我明白了
标签: java arrays hashmap integer