Java消除了数组中的重复项

时间:2015-04-17 17:46:50

标签: java arrays

无法使其正常工作,它应该对用户输入的任意数量的数字进行排序,然后消除重复数据。现在程序只打印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;
    }
}

3 个答案:

答案 0 :(得分:1)

您实际上并未将值复制过来。

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;

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

int[] newList = new int[numOfNums];

在您尝试打印内容之前,永远不会再次访问它,默认情况下,这些内容全部为零。

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

newList = eliminateDuplicates(list);

但这一切都不重要,因为您对数组的修改是 destructive ,这意味着在调用remove duplicates方法之后您的数组会有所不同。这是固定的程序:

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

答案 1 :(得分:1)

你需要包含它来在你的removeuplicates函数中返回正确的int数组:

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

然后你也需要抓住它:

我的意思是代替这个:

 eliminateDuplicates(list);

你需要这个:

 list = eliminateDuplicates(list);

答案 2 :(得分:-2)

public static void sortList(int[] list)

while(numOfNums != -1 && x < list.length)

eliminateDuplicates(list);

这些是我从你的代码中挑选出来的三件事,请注意你我也很快就把它们拿走了。我们将从顶部开始:

public static void sortList(int[] list)

字面意义不做任何事。 为什么?简单!它什么都不返回。因为它确实返回SOMETHING,它需要返回类型为int[]

while(numOfNums != -1 && x < list.length)

当像你一样初始化数组时,你在括号内给它array.length值。包含0个项目的列表毫无价值到您的代码中。哎呀,一个包含1个项目的列表对你的代码毫无价值。所以也许可以改为:

while(numOfNums > 1 && x < list.length)

最后:

eliminateDuplicates(list);

这个,也什么也没做。再一次,为什么?唉,再一次,它很简单。它可能会返回int[],但您不会对它返回的内容做任何事情。要使用它,请将其更改为:

array = eliminateDuplicates(list);

或者可能更适合您的情况:

newList = new int[eliminateDuplicates(list).length]; newList = eliminateDuplicates(list);

这会将其设置为列表的确切大小,然后将其设置为等于返回的数组。

相关问题