簡體   English   中英

如何在不重復的情況下隨機選擇通用類型

[英]How do I randomly select a generic type without repetitions

我有一個編碼作業,我已經完成了大約 90%。 任務是將座位隨機分配給隨機選擇的人。 我在 Generic 類中的 drawItem() 方法遇到問題,因為我應該消除它,因此在繪制時不會重復相同的數字或人名。

賦值描述說:drawItem()——從框中隨機選擇一個對象並返回它,以及從框中刪除它(如果框為空返回null)......

非常感謝您對消除對象的任何幫助!! 我已經被這部分作業困住了太久了。

這是我的 GenericDriver 類:


    public static void main(String[] args) {

        Generic<String> myStringBox = new Generic<String>();

        // use the .add() function to add slips of papers with names on
        // them to fill the box
        myStringBox.add("Person 1");
        myStringBox.add("Person 2");
        myStringBox.add("Person 3");
        myStringBox.add("Person 4");
        myStringBox.add("Person 5");
        System.out.println("... names are being added!");

        Generic<Integer> myIntegerStack = new Generic<Integer>();
        // use the .add() function to add the integer slips of paper to the box
        myIntegerStack.add(1);
        myIntegerStack.add(2);
        myIntegerStack.add(3);
        myIntegerStack.add(4);
        myIntegerStack.add(5);
        System.out.println("... numbers are being added!");

        System.out.println("..... Generating which seat a person will occupy .....\n");

        System.out.println("First draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Second draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Third draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Fourth draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Fifth draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

    }

} 

這是我的通用類:

import java.util.Arrays;
import java.io.*;
import java.util.*;
import java.util.Collections;
@SuppressWarnings("unchecked")

public class Generic<T> {

    // creating an array
    T[] item;
    // count of the object's size
    int size;

    //generic constructor that will initilize the items in the box
    Generic (){
        item = (T[]) new Object[5];
    }

    public void add(T t) {
        item[size] = t;
        size++;
    }

    Random randomDraw = new Random();

    public T drawItem() {
        return item[randomDraw.nextInt(item.length)];
    }

    public String toString(){
        return Arrays.toString(item);
    } 

這是一個示例輸出,用於可視化我的意思:

..... Generating which seat a person will occupy .....

First draw: Person 4 will occupy seat number: 1
Second draw: Person 5 will occupy seat number: 1
Third draw: Person 2 will occupy seat number: 1
Fourth draw: Person 3 will occupy seat number: 3
Fifth draw: Person 1 will occupy seat number: 5

drawItem() 應該從底層數組中刪除項目,這樣就不會再次選擇它。 它的字面意思是刪除項目並返回它,或者如果數組為空則返回 null。

這應該為你做。 泛型也使用 T 的列表而不是數組。 如果這不可能,請告訴我。

    public static class Generic<T> {

        Random random = new Random();

        List<T> items;

        public Generic(List<T> items) {
            this.items = items;
        }

        public Generic(T... items) {
            this(new ArrayList<>(Arrays.asList(items)));
        }

        public void add(T t) {
            items.add(t);
        }

        public T drawItem() {
            if (items.size() <= 0) {
                return null;
            }
            int index = random.nextInt(items.size());

            return items.remove(index);
        }

        public String toString() {
            return items.toString();
        }
    }

測試

    public static void main(String[] args) {
        Generic<String> myStringBox = new Generic<>("Person 0", "Person 1", "Person 2", "Person 3", "Person 4");

        Generic<Integer> myIntegerStack = new Generic<>(0, 1, 2, 3, 4);

        for (int index = 0; index < myStringBox.items.size(); index++) {
            String person = myStringBox.drawItem();

            int seat = myIntegerStack.drawItem();

            System.out.println(String.format("%s draw: %s will occupy seat %s.",
                    (index + 1), person, seat));
        }
    }

輸出:

1 draw: Person 4 will occupy seat 1.
2 draw: Person 0 will occupy seat 3.
3 draw: Person 1 will occupy seat 4.
4 draw: Person 3 will occupy seat 0.
5 draw: Person 2 will occupy seat 2.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM