java:洗牌,
这是一个面试问题。请给一些提示:
用向量实现一个方法,洗一副牌。
public class Card {
private int value;
Card(int v) {
value = v;
}
public void print(){
System.out.print(value+";");
}
}
public class DeckShuffle {
private final int num;
Vector<Card> deck = new Vector<Card>();
// implement this shuffle function. DO NOT USE Collections.shuffle() !!
public void shuffle(){
// your code goes here!
}
}
This is a interview-questions. Please give some hints:
Use vector to implement a method, shuffle a deck of Card.
public class Card {
private int value;
Card(int v) {
value = v;
}
public void print(){
System.out.print(value+";");
}
}
public class DeckShuffle {
private final int num;
Vector<Card> deck = new Vector<Card>();
// implement this shuffle function. DO NOT USE Collections.shuffle() !!
public void shuffle(){
// your code goes here!
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以在 JDK 或 OpenJDK 的源包中找到,但算法非常简单(对于集合和数组来说基本上是相同的):
Collections.shuffle()
的代码 就位,因此您不需要额外的并行内存。它被称为Fisher Yates shuffle。The code for
Collections.shuffle()
can be found in the source bundle for the JDK or from OpenJDK, but the algorithm is pretty simple (basically the same for a collection as for an array):This works in place so you don't need extra parallel memory. It is known as the Fisher Yates shuffle.
我想到的是:
这是干编码,没有进行测试。
Here's what comes to mind :
This is dry coded, no testing done.