如何在不重复的情况下随机选择通用类型

huangapple go评论157阅读模式
英文:

How do I randomly select a generic type without repetitions

问题

这是您提供的代码和说明的翻译:

我有一个编码作业我完成了大约90%任务是随机为随机选定的人分配座位我在Generic类中的drawItem()方法上遇到了问题因为我应该消除它这样在抽取时不会重复出现相同的编号或人名

作业描述如下drawItem() - 随机从盒子中选择一个对象并返回它同时从盒子中消除它如果盒子为空则返回null...

*非常感谢任何有关消除对象的帮助* 我在这部分作业上卡了很长时间了

这是我的GenericDriver类

```java
public class GenericDriver<T> {

    public static void main(String[] args) {

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

        // 使用.add()函数添加带有名称的纸片来填充盒子
        myStringBox.add("Person 1");
        myStringBox.add("Person 2");
        myStringBox.add("Person 3");
        myStringBox.add("Person 4");
        myStringBox.add("Person 5");
        System.out.println("...正在添加名称!");

        Generic<Integer> myIntegerStack = new Generic<Integer>();
        // 使用.add()函数将整数纸片添加到盒子中
        myIntegerStack.add(1);
        myIntegerStack.add(2);
        myIntegerStack.add(3);
        myIntegerStack.add(4);
        myIntegerStack.add(5);
        System.out.println("...正在添加数字!");

        System.out.println("..... 生成哪个座位将有人占用 .....\n");

        System.out.println("第一次抽取: " + myStringBox.drawItem()
            + " 将占用座位编号: " + myIntegerStack.drawItem());

        System.out.println("第二次抽取: " + myStringBox.drawItem()
            + " 将占用座位编号: " + myIntegerStack.drawItem());

        System.out.println("第三次抽取: " + myStringBox.drawItem()
            + " 将占用座位编号: " + myIntegerStack.drawItem());

        System.out.println("第四次抽取: " + myStringBox.drawItem()
            + " 将占用座位编号: " + myIntegerStack.drawItem());

        System.out.println("第五次抽取: " + myStringBox.drawItem()
            + " 将占用座位编号: " + myIntegerStack.drawItem());

    }

}

这是我的Generic类:

import java.util.Random;
import java.util.Arrays;

public class Generic<T> {

    // 创建一个数组
    T[] item;
    // 对象大小的计数
    int size;

    // 泛型构造函数,将初始化盒子中的项目
    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);
    }
}

这是一个可视化示例输出:

..... 生成哪个座位将有人占用 ......
第一次抽取: Person 4 将占用座位编号: 1
第二次抽取: Person 5 将占用座位编号: 1
第三次抽取: Person 2 将占用座位编号: 1
第四次抽取: Person 3 将占用座位编号: 3
第五次抽取: Person 1 将占用座位编号: 5
英文:

I have a coding assignment which I'm about 90% of the way through. The task is to randomly assign seats to a randomly selected person. I am having troubles with my drawItem() method in the Generic class, as I am supposed to eliminate it so the same number or person name doesn't repeat when being drawn.

The assignment description says: drawItem() – to randomly select an object from the box and return it, as well as eliminating it from the box (if the box is empty return null)...

Any help with the eliminating the object is greatly appreciated!! I have been stuck on this part of the assignment for too long now.

Here is my GenericDriver class:


public static void main(String[] args) {
Generic&lt;String&gt; myStringBox = new Generic&lt;String&gt;();
// use the .add() function to add slips of papers with names on
// them to fill the box
myStringBox.add(&quot;Person 1&quot;);
myStringBox.add(&quot;Person 2&quot;);
myStringBox.add(&quot;Person 3&quot;);
myStringBox.add(&quot;Person 4&quot;);
myStringBox.add(&quot;Person 5&quot;);
System.out.println(&quot;... names are being added!&quot;);
Generic&lt;Integer&gt; myIntegerStack = new Generic&lt;Integer&gt;();
// 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(&quot;... numbers are being added!&quot;);
System.out.println(&quot;..... Generating which seat a person will occupy .....\n&quot;);
System.out.println(&quot;First draw: &quot; + myStringBox.drawItem()
+ &quot; will occupy seat number: &quot; + myIntegerStack.drawItem());
System.out.println(&quot;Second draw: &quot; + myStringBox.drawItem()
+ &quot; will occupy seat number: &quot; + myIntegerStack.drawItem());
System.out.println(&quot;Third draw: &quot; + myStringBox.drawItem()
+ &quot; will occupy seat number: &quot; + myIntegerStack.drawItem());
System.out.println(&quot;Fourth draw: &quot; + myStringBox.drawItem()
+ &quot; will occupy seat number: &quot; + myIntegerStack.drawItem());
System.out.println(&quot;Fifth draw: &quot; + myStringBox.drawItem()
+ &quot; will occupy seat number: &quot; + myIntegerStack.drawItem());
}
} 

Here is my Generic class:

import java.util.Arrays;
import java.io.*;
import java.util.*;
import java.util.Collections;
@SuppressWarnings(&quot;unchecked&quot;)
public class Generic&lt;T&gt; {
// creating an array
T[] item;
// count of the object&#39;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);
} 

Here is a sample output to visualize what I mean:

..... 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

答案1

得分: 1

drawItem()应该从底层数组中移除该项,以便不会再次选择它。它明确说明要移除该项并返回它,如果数组为空则返回null。

这应该适用于您。泛型还使用了List of 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.
英文:

drawItem() should remove the item from the underlying array so it's not chosen again. It literally says to remove the item and return it or return null if the array is empty.

This should do it for you. Generic also uses List of T instead of array. If this isn't possible let me know.

    public static class Generic&lt;T&gt; {

        Random random = new Random();

        List&lt;T&gt; items;

        public Generic(List&lt;T&gt; items) {
            this.items = items;
        }

        public Generic(T... items) {
            this(new ArrayList&lt;&gt;(Arrays.asList(items)));
        }

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

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

            return items.remove(index);
        }

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

Test

    public static void main(String[] args) {
        Generic&lt;String&gt; myStringBox = new Generic&lt;&gt;(&quot;Person 0&quot;, &quot;Person 1&quot;, &quot;Person 2&quot;, &quot;Person 3&quot;, &quot;Person 4&quot;);

        Generic&lt;Integer&gt; myIntegerStack = new Generic&lt;&gt;(0, 1, 2, 3, 4);

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

            int seat = myIntegerStack.drawItem();

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

Output:

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.

huangapple
  • 本文由 发表于 2020年3月4日 10:27:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/60518243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定