随机选择一个字符串并将其放入堆栈?

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

Randomly taking a string and putting it into a Stack?

问题

我目前有一个包含52个元素的ArrayList<String>。每个元素代表一副不同的牌。我正尝试随机从ArrayList中抽取一张“牌”,并将其放入一个堆栈中,但遇到了一些问题。我该如何使用Random rand = new Random();从ArrayList中随机抽取表示一张牌的字符串,并将其存储在一个堆栈中?

英文:

I currently have an ArrayList<String> with 52 elements. Each it a different card from a deck. I am trying to randomly pull a "card" from ArrayList and put it into a stack but am having some trouble. How would I use Random rand = new Random(); to pull a string indicating a card and store it in a stack?

答案1

得分: 3

List<Items> items = ...
Collections.shuffle(items);

现在按顺序从列表中取出物品。它们将会从洗牌后的状态中随机出现。

你也可以将自己创建的 Random 实例作为 shuffle 的第二个参数传递。这在指定种子时非常有用。然后它将始终以相同的方式进行洗牌。这对于测试很有用。


另一种方法是创建一个包装类来对一个 List 进行随机返回值。它会创建传入列表的防御性副本,以避免改变所提供的列表,并确保不会重复任何值。与 Collections.shuffle() 一样,你可以将 Random 实例作为第二个参数传递。

List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
RandomList<Integer> rlist = new RandomList<>(list);
rlist.forEach(a -> System.out.print(a + " "));

此实例打印:

3 1 5 10 7 8 4 6 2 9
class RandomList<T> implements Iterable<T> {
	private int count;
	private List<T> list;
	private Random r;
	
	public RandomList(List<T> list) {
		// 创建传入列表的防御性副本
		this(list, new Random());
	}
	
	public RandomList(List<T> list, Random rand) {
	   this.list = new ArrayList<T>(list);
	   this.r = rand;
	   count = list.size();
	}
	
	private class MyIterator implements Iterator<T> {
		public boolean hasNext() {
			return count > 0;
		}
		
		public T next() {
			// 获取随机索引
			int i = r.nextInt(count--);
			// 并获取该值
			T v = list.get(i);
			// 此值现在被“使用”
			// 所以用列表中的最后一个值替换它。
			list.set(i, list.get(count));
			return v;
		}
	}
	
	public Iterator<T> iterator() {
		return new MyIterator();
	}
}
英文:
List&lt;Items&gt; items = ...
Collections.shuffle(items);

Now just take the items off the list in order. They will be randomized from the shuffle.

You can also pass your own instance of Random as a second argument to shuffle. That is primarily useful when specifying a seed. Then it will always shuffle the same way. It's good for testing.


A different approach is to create a wrapper class for a List to return the values randomly. It makes a defensive copy of the list to avoid permuting the supplied list and guarantees that no value will be repeated. And like Collections.shuffle() you can supply an instance of Random as the second argument.

		List&lt;Integer&gt; list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		RandomList&lt;Integer&gt; rlist = new RandomList&lt;&gt;(list);
		rlist.forEach(a-&gt;System.out.print(a + &quot; &quot;));

This instance printed

3 1 5 10 7 8 4 6 2 9 

class RandomList&lt;T&gt; implements Iterable&lt;T&gt; {
	private int count;
	private List&lt;T&gt; list;
	private Random r;
	
	public RandomList(List&lt;T&gt; list) {
		// make a defensive copy of passed list.
		this(list, new Random());
	}
	
	public RandomList(List&lt;T&gt; list, Random rand) {
	   this.list = new ArrayList&lt;T&gt;(list);
	   this.r = rand;
	   count = list.size();
	}
	private class MyIterator implements Iterator&lt;T&gt; {
		public boolean hasNext() {
			return count &gt; 0;
		}
		
		public T next() {
			// get a random index
			int i = r.nextInt(count--);
			// and retrieve that value
			T v = list.get(i);
			// that value is now &quot;used&quot;
			// so replace it with the last
			// value in the list.
			list.set(i, list.get(count));
			return v;
		}
	}
	
	public Iterator&lt;T&gt; iterator() {
		return new MyIterator();
	}
}

</details>



# 答案2
**得分**: 0


@WJS提出了一个解决方案,但如果你想使用`Random`,那么你可以使用以下方法:

从ArrayList中获取一个随机字符串,找到一个随机索引,获取该元素并将其放入堆栈,然后从ArrayList中删除该元素。

```java
Random rand = new Random(); 
int index=rand.nextInt(list.size());
String card=list.get(index);
stack.push(card);
list.remove(index);
英文:

@WJS has suggested a solution but if you want to use Random then you can use

To pull a random string from ArrayList, find a random index and get the element and put it into stack and remove that element from the arraylist

Random rand = new Random(); 
int index=rand.nextInt(list.size());
String card=list.get(index);
stack.push(card);
list.remove(index);

答案3

得分: 0

如果您使用 rand.nextInt(52);
它会随机生成一个介于 0 和 51 之间的值(两者都包括在内)。

您如何从您的数据中获取第1张、第n张或第52张卡?

英文:

If you use rand.nextInt(52);
It randomly draws a value between 0 and 51 (both included).

How can you get the 1st or nth or 52th card from your data ?

huangapple
  • 本文由 发表于 2020年4月8日 04:20:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61088749.html
匿名

发表评论

匿名网友

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

确定