【问题标题】:Java eliminating duplicates in an ArrayJava消除数组中的重复项
【发布时间】:2015-06-24 17:08:01
【问题描述】:

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

【问题讨论】:

  • 附带说明:您应该调试代码以真正了解它在做什么。例如:在main 中,嵌套循环有一些冗余。 for ( int x = 0; x &lt; list.length; ++x) 只会执行一次,因为嵌套的 while(numOfNums != -1 &amp;&amp; x &lt; list.length) { ... ++x; } 将处理所有迭代。

标签: java arrays


【解决方案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;

我相信你认为这行得通的原因是你在你的 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;
  }
}

【讨论】:

  • 非常感谢,解释的很好我现在明白了。
【解决方案2】:

您需要包含它以在您的消除重复函数中返回正确的 int 数组:

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

然后你也需要抓住它:

我的意思不是这个:

 eliminateDuplicates(list);

你需要这个:

 list = eliminateDuplicates(list);

【讨论】:

    【解决方案3】:

    public static void sortList(int[] list)

    while(numOfNums != -1 &amp;&amp; x &lt; list.length)

    eliminateDuplicates(list);

    这些是我从你的代码中挑选出来的 3 件事,请注意,我也很快就挑选出来了。我们将从头开始:

    public static void sortList(int[] list)

    实际上什么都不做。 为什么?简单!它什么也不返回。因为它确实返回了一些东西,所以在你的情况下它需要有一个 int[] 的返回类型。

    while(numOfNums != -1 &amp;&amp; x &lt; list.length)

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

    while(numOfNums &gt; 1 &amp;&amp; x &lt; list.length)

    最后:

    eliminateDuplicates(list);

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

    array = eliminateDuplicates(list);

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

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

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

    【讨论】:

    • sortList 方法确实有作用,因为对数组的修改是破坏性的。
    • @Nicholas 看在您的份上,您应该了解具有“无效”功能的含义。它不会“什么都不做”stackoverflow.com/questions/2390063/…
    • @matrixanomaly 在他的情况下确实如此。他使用的是局部变量,因此他没有编辑任何他将实际使用的东西。
    • @durron597 当我什么都不说时,我的意思是这对他没有任何帮助。如果它有代码行,它显然会做一些事情。
    • 大声笑没有。仅仅因为他不返回任何东西并不意味着它什么都不做。他正在改变事情,不管有没有帮助,正在执行一个程序。包括我在内的许多人都认为你的字面意思是“什么都不做”(因此投反对票)。您可能想编辑您的问题以说“方法没有按照您的想法做......等等”。它正在做某事。没什么。那里的方法正在尝试对列表进行排序。
    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 2014-04-23
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2023-03-20
    • 2023-03-10
    相关资源
    最近更新 更多