【问题标题】:Trying to Print Array without Repeating Numbers尝试打印数组而不重复数字
【发布时间】:2016-02-10 03:50:24
【问题描述】:

我有一个编程任务,我的任务是:

我采用两个 int 值(x 和 y)并创建两个不同的数组:第一个(大小 x)将打印一个从 x 开始并下降到 1 的数组。第二个(大小 y)将从第一个数组(大小 x)并将其存储在自己的数组中。然后我将打印出第二个数组。但是,第二个数组不能有任何重复值。例如,如果数组大小为 10,则它的 10 个单独索引中不能有两个相同的数字。我试图通过创建两个数组在我的第二个数组中存储唯一元素,一个用于检查唯一元素的布尔值,另一个用于存储这些唯一元素。这是我的代码:

/*
* user will enter desired size x for first array labeled arr_1
* arr_1 will contain values descending from x down to 1
* user will enter desired size y for second array labeled arr_2
* arr_2 will contain random values taken from arr_1 w/o repeating numbers
*/

import java.util.Arrays;
// import java.util.Arrays;
import java.util.Random;
// import java.util.Scanner;
public class Prog1B  
{
    public static void main(String[] args)
    {
        System.out.println("Program 1B, Christopher Moussa, masc1574");
        // Scanner scnr = new Scanner(System.in); 
        int x = 20;
        int v = x;
        int[] arr_1 = new int[x];

        for (int i = x-1; i >= 0; i--)
        {
            arr_1[i] = v;   // System.out.print(i+1 + " "); prints 20, 19, ... , 1
            v--;            // System.out.print(arr_1[i] + " "); prints 20, 19, ... , 1
        }
        // int[] b = unique(arr_1);
        System.out.println(Arrays.toString(unique(arr_1)));
    }

    public static int[] unique (int[] n)
    {
        boolean[] seen = new boolean[n.length];
        int[] unique = new int[n.length];
        Random rand = new Random(123L);
        for (int i = 0; i < n.length; i++)
        {
            int index = rand.nextInt(n.length);
            while (seen[index])
            {
                index = rand.nextInt(n.length);
            }
            unique[i] = n[index];
        }
        return unique;
    }



}

代码编译并运行,但它仍然打印出一个包含重复值的数组。我正在尝试编写程序,以便它不会打印出具有重复值的数组,而只会打印出唯一值。您对问题所在有什么建议吗?我很确定它位于“唯一”方法中,更具体地说,当布尔数组正在检查唯一值时(我在尝试调试时注意到,即使它生成的随机索引不是唯一的,它仍然跳过了 while 条件和打印出来)。我是一名初级程序员(圣地亚哥州立大学学习计算机科学的大一新生),任何反馈/建议将不胜感激。非常感谢你。

【问题讨论】:

  • 使用随机索引的目的是什么?
  • 为什么不使用一套呢?集合不能有重复项。
  • 大家这不是 Chegg,不要为 Christopher 解决问题,按照meta.stackexchange.com/questions/10811/… 引导他朝着正确的方向前进
  • @JimLohse 您如何建议我们“引导他走向正确的方向”?他有一个简单的问题,我们向他展示了哪里出了问题。他只是犯了一个错误,这并不能真正反映他的方法的质量。
  • @Benjamin 如果您阅读我发布的链接,您会看到如何,我没有写政策,我只是尝试遵循它:)

标签: java arrays unique-values


【解决方案1】:

我在您的代码中发现了问题。你永远不会更新你的“看到的”布尔数组。请参阅下面的代码进行修复:

public static int[] unique (int[] n){
 boolean[] seen = new boolean[n.length];
 int[] unique = new int[n.length];
 Random rand = new Random(123L);
 for (int i = 0; i < n.length; i++)
 {
     int index = rand.nextInt(n.length);
     while (seen[index])
     {
         index = rand.nextInt(n.length);
     }
     seen[index] = true; //boolean array updated
     unique[i] = n[index];
 }
 return unique;

}

使用此修复程序,我能够获得以下输出(没有重复):

[3、11、17、10、16、18、15、6、14、20、7、13、1、19、9、2、5、4、12、8]

【讨论】:

  • 我继续将我的DV更改为UP,只需阅读我发布的关于如何回答作业的链接,也许只是在作业到期后提出建议,我将在评论中引用相关文字对 Sanchita 的回答。
  • 非常感谢本杰明·洛瑞!我能够更新我的代码,它的运行方式和你的一样。
【解决方案2】:

你甚至需要设置你的数组 seen[index] = true;

public static int[] unique (int[] n)
    {
        boolean[] seen = new boolean[n.length];
        int[] unique = new int[n.length];
        Random rand = new Random(123L);
        for (int i = 0; i < n.length; i++)
        {
            int index = rand.nextInt(n.length);
            while (seen[index])
            {
                index = rand.nextInt(n.length);
            }
            unique[i] = n[index];
            seen[index] = true;
        }
        return unique;
    }

【讨论】:

  • 将我的 DV 更改为 UV,但根据 Benjamin 的回答,“如果您认为这对学生没有帮助,最好不要提供完整的代码示例,使用您的最佳判断。您可以使用伪-code first,并且,本着创建编程资源的精神,您可能会在适当的时间后回来并编辑您的响应以包含更完整的代码。这样,学生仍然必须编写自己的代码,但是任务结束后可以得到完整的解决方案。”来自meta.stackexchange.com/questions/10811/…
【解决方案3】:

除非您特别需要这样做,否则我建议您退后一步,尝试一种完全不同的方法,如下所示:

Set<int> mySet = new HashSet<int>(Arrays.asList(someArray));

注意:您需要将 unique() 的返回类型调整为 Set

其余的实现留给读者练习。基本上你把数组转换成上面例子的集合。

(Credit where credit is due)

我只是想根据https://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions 引导您朝着正确的方向前进

祝你好运,我想说这里最大的教训是,当存在更好的解决方案时,如何摆脱效率低下的代码。祝你好运!

【讨论】:

  • 在考虑如何解决这个问题时,我也在考虑这种方法;我联系了我的老师关于使用哈希集,她说她宁愿我不使用它(我们还没有讨论集)。非常感谢您的建议,我一定会好好利用它的。
  • @ChristopherMoussa 是的,感谢我链接的那个文档,它指出,家庭作业通常会带来限制,很好的思考!比课程计划提前一步对你有好处:)
  • @Christopher 并且不要忘记接受其他答案之一,根据这些指南,实际上解决了您的问题(不是我的哈哈)stackoverflow.com/help/someone-answers“当有人回答时我该怎么办我的问题”
【解决方案4】:

这里是如何使用 java8 lambdas 来做到这一点

    ArrayList<Integer> arrayli = new ArrayList<Integer>(Arrays.asList(arr_1));//converted array to list
    System.out.println();
    List<Integer> distinctIntegers = arrayli.stream().
    .distinct()
    .boxed()
    .collect(Collectors.toList());
 distinctIntegers.foreach(System.out::println);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-29
    • 2022-11-20
    • 2018-04-20
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    相关资源
    最近更新 更多