0

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

我正在获取两个 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 条件和打印出来)。我是一名初级程序员(圣地亚哥州立大学学习计算机科学的大一新生),任何反馈/建议将不胜感激。非常感谢你。

4

4 回答 4

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;
    }
于 2016-02-10T04:08:16.140 回答
2

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

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]

于 2016-02-10T04:09:16.870 回答
1

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

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

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

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

信用到期的信用

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

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

于 2016-02-10T04:06:39.140 回答
0

下面是如何使用 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);
于 2016-02-10T13:07:45.117 回答