hashset从数组Java中删除重复项

时间:2016-03-27 09:27:58

标签: java duplicates

我目前正试图让我的java程序在打印输出时不打印重复的法律。该软件是随机挑选打印1至3法律,但我不想重复。我还是java的新手,到目前为止只编写了一些程序。这是我到目前为止写的最复杂的一个。

现在我希望软件再次运行随机,如果它找到重复,直到打印中没有重复。反正有没有做到这一点?

/**
    *Seeds is a random number to pick which if statement will be used. 
    * telling the software how many laws to use during its random 
    * generation process
    * 
    */
    Random seeds = new Random(); 
    seed = seeds.nextInt(3) + 1; 

    if (seed == 1){


        /**Beginning of random law selection for universe. Must find way 
         *Must find way to get rid of duplicate answers 
         */
        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index]);
    }

    else if (seed == 2) {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index] + "," + Laws[index2]);

    }

    else {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            int index3 = random.nextInt(Laws.length);
            System.out.println("Law: " + Laws[index] + "," + Laws[index2] + 
                    "," + Laws[index3]);

    }

2 个答案:

答案 0 :(得分:2)

另一种思考这个问题的方法是:您想要以随机顺序选择值。这样,除非元素出现多次,否则不会出现重复。

List<String> laws = Arrays.asList("Standard Physics", "Magic", "Mad Science", 
        "Psionics", "Substandard Physics","Exotic");
Collections.shuffle(laws);
// select as many as you need
List<String> lawsSelected = laws.subList(0, 3);

答案 1 :(得分:0)

这是一种方法。如果条件如此,则需要3个布尔变量,并将每个布尔变量与if关联,并在执行if时将其设为true。

Random seeds = new Random(); 
    seed = seeds.nextInt(3) + 1; 


boolean selected[] = new boolean[3];
selected[0] = selected[1] = selected[2] = false;//initially none of the if is executed

    if (seed == 1 && !selected[seed-1]){//checking if not previously selected and "seed-1" since array indexes are from 0 to n-1


        /**Beginning of random law selection for universe. Must find way 
         *Must find way to get rid of duplicate answers 
         */
        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index]);
            selected[seed-1] = true;
    }

    else if (seed == 2 && !selected[seed-1]) {

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            System.out.println("Law:" + Laws[index] + "," + Laws[index2]);
            selected[seed-1] = true;
    }

    else if (!selected[seed-1]){

        final String[] Laws = {"Standard Physics", "Magic", "Mad Science", 
            "Psionics", "Substandard Physics","Exotic"};
            Random random = new Random();
            int index = random.nextInt(Laws.length);
            int index2 = random.nextInt(Laws.length);
            int index3 = random.nextInt(Laws.length);
            System.out.println("Law: " + Laws[index] + "," + Laws[index2] + 
                    "," + Laws[index3]);
        selected[seed-1] = true;
    }