英文:
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<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());
}
}
Here is my Generic class:
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);
}
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<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();
}
}
Test
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));
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论