我有一个问题,如何删除重复值的数组,有人能帮我吗?我有这个代码只是为了合并2个数组和显示结果,但我不知道如何删除重复的输入(对不起,我刚开始学java。)
public static void main(String... args) {
int[] x = new int[3];
int[] y = new int[3];
int[] xy = new int[6];/***new array to hold the integers of arrays x and y****/
int temp, c = 0;
Scanner myInput = new Scanner(System.in);
/***input of array x at the same time storing the integers to array xy***/
System.out.println("enter 3 integers for array x:");
for (int a = 0; a < 3; a++) {
x[a] = myInput.nextInt();
xy[c] = x[a];
c++;
}
/***input of array y at the same time storing the integers to array xy***/
System.out.println("enter 3 integers for array y:");
for (int b = 0; b < 3; b++) {
y[b] = myInput.nextInt();
xy[c] = y[b];
c++;
}
/*sorting...*/
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5 - i; j++) {
if (xy[j] > xy[j + 1]) {
temp = xy[j];
xy[j] = xy[j + 1];
xy[j + 1] = temp;
}
}
}
/*printing of array xy sorted*/
for (int w = 0; w < 6; w++)
System.out.print(xy[w] + " ");
}
1条答案
按热度按时间xienkqul1#
由于已对合并数组进行排序,因此简化的解决方案是在打印时不删除重复项,而是通过比较循环中的当前值和前一个值来过滤掉它们