gpt4 book ai didi

Java消除数组中的重复项

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:09 25 4
gpt4 key购买 nike

无法正常工作,它应该对用户输入的任意数量的数字进行排序,然后消除重复项。现在程序只打印 0,但它应该打印一个没有重复项的数组。我也必须以困难的方式做到这一点,我不能使用 Java 内置的排序或数组复制方法。 为什么我的代码只打印 0,如何修复它?

package Lab_10;

import java.util.Scanner;
public class Eliminating_duplicates
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of numbers: ");
int numOfNums = input.nextInt();
int[] list = new int[numOfNums];
int[] newList = new int[numOfNums];
for( int x = 0; x < list.length; ++x)
{
while(numOfNums != -1 && x < list.length)
{
System.out.print("Enter value " + (x + 1) + ": ");
int value = input.nextInt();
list[x] = value;
++x;
}
}
sortList(list);
System.out.println("Here is the sorted list: ");
for (int x = 0; x < list.length; ++x)
{
System.out.println(list[x]);
}
eliminateDuplicates(list);
System.out.println("Here is the list without duplicates: ");
for (int x = 0; x < newList.length; ++x)
{
System.out.println(newList[x]);
}
}
public static void sortList(int[] list)
{
int temp;
boolean madeASwap = true;
int lastIndex = list.length-1;
while (madeASwap)
{
madeASwap = false;
for (int x = 0; x < lastIndex; ++x)
{
if (list[x] > list[x + 1])
{
temp = list[x];
list[x] = list[x + 1];
list[x + 1] = temp;
madeASwap = true;
}
}

}
}
public static int[] eliminateDuplicates(int[] list)
{
int end = list.length;
for (int i = 0; i < end; i++)
{
for (int j = i + 1; j < end; j++) {
if (list[i] == list[j]) {
for (int k = j + 1; k < end; k++, j++) {
list[j] = list[k];
}
--end;
--j;
}
}
}
int[] newList = new int[end];
return newList;
}
}

最佳答案

您实际上并没有复制这些值。

int[] newList = new int[end];
return newList;

这将创建一个仅包含零的新列表,该列表将被忽略。您可以使用以下内容复制它们:

int[] newList = new int[end];
for (int i = 0; i < end; i++) {
newList[i] = list[i];
}
return newList;

我相信,您认为这有效的原因是您在您的 main 方法中有一个名为 newList 的数组,但您从未为其分配任何内容。

int[] newList = new int[numOfNums];

除非您尝试打印其内容,否则永远不会再次访问它,默认情况下,这些内容再次全为零。

确保通过执行以下操作保存返回的数组:

newList = eliminateDuplicates(list);

不过,这些都不重要,因为您对数组的修改是破坏性的,这意味着调用消除重复方法后您的数组会有所不同。这是固定的程序:

import java.util.Scanner;

public class Eliminating_duplicates {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of numbers: ");
int numOfNums = input.nextInt();
int[] list = new int[numOfNums];
int[] newList = new int[numOfNums];
for (int x = 0; x < list.length; ++x) {
while (numOfNums != -1 && x < list.length) {
System.out.print("Enter value " + (x + 1) + ": ");
int value = input.nextInt();
list[x] = value;
++x;
}
}
sortList(list);
System.out.println("Here is the sorted list: ");
for (int x = 0; x < list.length; ++x) {
System.out.println(list[x]);
}
newList = eliminateDuplicates(list);
System.out.println("Here is the list without duplicates: ");
for (int x = 0; x < newList.length; ++x) {
System.out.println(newList[x]);
}
}

public static void sortList(int[] list) {
int temp;
boolean madeASwap = true;
int lastIndex = list.length - 1;
while (madeASwap) {
madeASwap = false;
for (int x = 0; x < lastIndex; ++x) {
if (list[x] > list[x + 1]) {
temp = list[x];
list[x] = list[x + 1];
list[x + 1] = temp;
madeASwap = true;
}
}

}
}

public static int[] eliminateDuplicates(int[] list) {
int end = list.length;
for (int i = 0; i < end; i++) {
for (int j = i + 1; j < end; j++) {
if (list[i] == list[j]) {
for (int k = j + 1; k < end; k++, j++) {
list[j] = list[k];
}
--end;
--j;
}
}
}
int[] newList = new int[end];
for (int i = 0; i < end; i++) {
newList[i] = list[i];
}
return newList;
}
}

关于Java消除数组中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29706132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com