Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何用相同的算法洗牌2个ArrayList?_Java_Algorithm_Shuffle - Fatal编程技术网

Java:如何用相同的算法洗牌2个ArrayList?

Java:如何用相同的算法洗牌2个ArrayList?,java,algorithm,shuffle,Java,Algorithm,Shuffle,我想用算法洗牌cardList和valueList,但当我对每一个使用洗牌集合时,它们使用不同的算法。所以,当我从牌组中抽出一张牌时,它将是一张随机的牌,并且同一张牌不会再次出现 public class BlackJack { public static void main(String[] args) { Random rand = new Random(); Scanner input =new Scanner(System.in);

我想用算法洗牌cardList和valueList,但当我对每一个使用洗牌集合时,它们使用不同的算法。所以,当我从牌组中抽出一张牌时,它将是一张随机的牌,并且同一张牌不会再次出现

public class BlackJack {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner input =new Scanner(System.in);
        ArrayList<String> cardList= new ArrayList<String>();
        ArrayList<String> cardShuffle= new ArrayList<String>();
        ArrayList<Integer> valueList= new ArrayList<Integer>();
        ArrayList<String> valueShuffle= new ArrayList<String>();

        cardList = card(cardList); 
        valueList = value(valueList);

        Collections.shuffle(cardList);
        Collections.shuffle(valueList);

        System.out.println(cardList);
        System.out.println(valueList);

    }
    private static ArrayList card(ArrayList<String> cardList){
        String cards = null;
        String suits = null;
        int cardCount = 0;
        for(int s = 1;s<5; s++){

            switch (s){
            case 1: suits = " of Heart"; break;
            case 2: suits = " of Diamond"; break;
            case 3: suits = " of Spades"; break;
            case 4: suits = " of Clubs"; break;
            }

            for(int c = 1;c<14; c++){
                switch (c){
                case 1: cards = "2"; break;
                case 2: cards = "3"; break;
                case 3: cards = "4"; break;
                case 4: cards = "5"; break;
                case 5: cards = "6"; break;
                case 6: cards = "7"; break;
                case 7: cards = "8"; break;
                case 8: cards = "9"; break;
                case 9: cards = "10"; break;
                case 10: cards = "Jack"; break;
                case 11: cards = "Queen"; break;
                case 12: cards = "King"; break;
                case 13: cards = "Ace"; break;
                }
                cardList.add(cardCount, cards + suits);
                cardCount++;
            }
        }

        return cardList;
    }
    private static ArrayList<Integer> value(ArrayList<Integer> valueList){

        int v = 2;
        int counter = 1;
        for(int c = 1;c<52; c++){
            if(v > 10){
                v = 10;
            }
            valueList.add(v);
            v++;
            counter++;
            if(counter>13){
                counter = 1;
                v=2;
            }
        }

        return valueList;
    }
}
公共类21点{
公共静态void main(字符串[]args){
Random rand=新的Random();
扫描仪输入=新扫描仪(System.in);
ArrayList cardList=新建ArrayList();
ArrayList cardShuffle=新的ArrayList();
ArrayList valueList=新的ArrayList();
ArrayList valueShuffle=新的ArrayList();
卡片列表=卡片(卡片列表);
valueList=值(valueList);
收藏。洗牌(卡片列表);
收藏。洗牌(价值清单);
系统输出打印项次(卡片列表);
系统输出打印项次(价值清单);
}
专用静态ArrayList卡(ArrayList cardList){
字符串卡=空;
字符串=null;
int cardCount=0;

对于(int s=1;s我认为在您的情况下,最好的解决方案是创建一个同时包含卡和值的类:

class ValuedCard {

    private String card;
    private int value;

    // constructor, getters/setters...
}

然后,您可以只拥有一个列表
ArrayList
并将其洗牌。

我认为对于您来说,最好的解决方案是创建一个同时包含卡和值的类:

class ValuedCard {

    private String card;
    private int value;

    // constructor, getters/setters...
}

然后,您可以只拥有一个列表
ArrayList
,并将其洗牌。

首先,请回答您的几个问题:

您可以使用映射来保持映射到卡的值。这样,您只需发送一张卡即可查找值

Map<String, Integer> cardsWithValues = new HashMap<>();
//Fill it by going cardsWithValue.put("2 of Dimonds", 2) for all cards. You can use a loop.

  //Shuffle the cards array, then you can get the values by doing:
   System.out.print(cardsWithValue.get(cards.get(i));
然后你可以将卡片存储在一个列表中,一切都正常。这可能是Java最常用的方法。你甚至可以给这个类一些实用函数。也许你只是想发送一套衣服和一个数字,而不是完整的字符串

最后一个选项是向shuffle方法发送种子

Long seed = 1234;
Random r = new Random(seed); 
Collections.shuffle(cardsList, r);
r = new Random(seed);
Collections.shuffle(valueList, r);
现在我们来看看其他的东西:对代码的注释

private static ArrayList card(ArrayList<String> cardList){
专用静态ArrayList卡(ArrayList cardList){
你给这个方法发送一个列表,你把所有的东西都添加到其中,然后你返回这个列表。你真的不必同时做这两件事。你可以只做两件事

    ArrayList<String> cardList= card(); 

 private static ArrayList card(){
        List<String> cardList = new ArrayList<String>
        //Fill it with stuff 
        return cardList;
    }
ArrayList cardList=card();
专用静态ArrayList卡(){
List cardList=新阵列列表
//装满东西
返回卡片列表;
}

或者您可以只发送列表而不返回。我更喜欢第一种方法,因为它更干净。

首先回答您几个问题:

您可以使用映射来保持映射到卡的值。这样,您只需发送一张卡即可查找值

Map<String, Integer> cardsWithValues = new HashMap<>();
//Fill it by going cardsWithValue.put("2 of Dimonds", 2) for all cards. You can use a loop.

  //Shuffle the cards array, then you can get the values by doing:
   System.out.print(cardsWithValue.get(cards.get(i));
然后你可以将卡片存储在一个列表中,一切都正常。这可能是Java最常用的方法。你甚至可以给这个类一些实用函数。也许你只是想发送一套衣服和一个数字,而不是完整的字符串

最后一个选项是向shuffle方法发送种子

Long seed = 1234;
Random r = new Random(seed); 
Collections.shuffle(cardsList, r);
r = new Random(seed);
Collections.shuffle(valueList, r);
现在我们来看看其他的东西:对代码的注释

private static ArrayList card(ArrayList<String> cardList){
专用静态ArrayList卡(ArrayList cardList){
你给这个方法发送一个列表,你把所有的东西都添加到其中,然后你返回这个列表。你真的不必同时做这两件事。你可以只做两件事

    ArrayList<String> cardList= card(); 

 private static ArrayList card(){
        List<String> cardList = new ArrayList<String>
        //Fill it with stuff 
        return cardList;
    }
ArrayList cardList=card();
专用静态ArrayList卡(){
List cardList=新阵列列表
//装满东西
返回卡片列表;
}

或者您可以只发送列表而不返回它。我更喜欢第一个方法,因为它更干净。

您可以使用的重载版本接受a作为参数,您可以通过该方法使用相同的种子对其进行初始化

因此,在您的代码中,每个卡组都需要一个
随机
,使用相同的种子,例如:

Random rand1 = new Random();
rand1.setSeed(123);

Random rand2 = new Random();
rand2.setSeed(123);

//..and further below

Collections.shuffle(cardList, rand1);
Collections.shuffle(valueList, rand2);
只需确保将这两者混合在一起(您需要更好的OO设计来封装它)

还要注意的是,在
Collection.shuffle()中,数组的大小正在被使用,因此在您的情况下,两个集合需要具有相同的大小

在上面的代码示例中,情况并非如此,因为存在一个bug:

在静态
value()
方法中,for循环需要包含52,换句话说:

for(int c = 1;c<=52; c++){

for(int c=1;c您可以使用的重载版本接受a作为参数,您可以通过方法使用相同的种子对其进行初始化

因此,在您的代码中,每个卡组都需要一个
随机
,使用相同的种子,例如:

Random rand1 = new Random();
rand1.setSeed(123);

Random rand2 = new Random();
rand2.setSeed(123);

//..and further below

Collections.shuffle(cardList, rand1);
Collections.shuffle(valueList, rand2);
只需确保将这两者混合在一起(您需要更好的OO设计来封装它)

还要注意的是,在
Collection.shuffle()中,数组的大小正在被使用,因此在您的情况下,两个集合需要具有相同的大小

在上面的代码示例中,情况并非如此,因为存在一个bug:

在静态
value()
方法中,for循环需要包含52,换句话说:

for(int c = 1;c<=52; c++){

for(int c=1;c你的值和你的牌在两个独立的数组列表中,所以当你洗牌时,它们会被不同的洗牌,不再对应。我的建议是创建一个名为Card的新类,有一个包含牌的数组列表,然后洗牌

下面是一个可能的卡片类别:

public class Card {

public static enum Suit {SPADE,HEART,DIAMOND,CLUB};

public static enum Value {

    //The integer parameter is the score. Adjust as needed.
    ACE(1),DEUCE(2),THREE(3),FOUR(4),FIVE(5),SIX(6),SEVEN(7),
    EIGHT(8),NINE(9),TEN(10),JACK(10),QUEEN(10),KING(10);
    int score;
    Value(int score) {
        this.score = score;
    }
}

private Suit suit;
private Value value;

public Card(Suit suit, Value value) {
    this.suit = suit;
    this.value = value;
}

public int getScore() {
    return value.score;
}

}
您可以创建如下所示的包(ArrayList):

ArrayList<Card> pack = new ArrayList<Card>();

for (Card.Value value : Card.Value.values()) {
    for (Card.Suit suit : Card.Suit.values()) {
        pack.add(new Card(suit, value));
    }
}

//finally, shuffle it:
Collections.shuffle(pack);
ArrayList pack=new ArrayList();
for(Card.Value值:Card.Value.values()){
对于(Card.Suit Suit:Card.Suit.values()){
包装。添加(新卡(套装、价值));
}
}
//最后,洗牌:
收藏。洗牌(打包);

您的值和卡位于两个单独的数组列表中,因此当您洗牌时,它们将以不同的方式洗牌,并且不再对应。我的建议是创建一个名为Card的新类,创建一个包含卡的单个数组列表,然后洗牌

下面是一个可能的卡片类别:

public class Card {

public static enum Suit {SPADE,HEART,DIAMOND,CLUB};

public static enum Value {

    //The integer parameter is the score. Adjust as needed.
    ACE(1),DEUCE(2),THREE(3),FOUR(4),FIVE(5),SIX(6),SEVEN(7),
    EIGHT(8),NINE(9),TEN(10),JACK(10),QUEEN(10),KING(10);
    int score;
    Value(int score) {
        this.score = score;
    }
}

private Suit suit;
private Value value;

public Card(Suit suit, Value value) {
    this.suit = suit;
    this.value = value;
}

public int getScore() {
    return value.score;
}

}
您可以创建如下所示的包(ArrayList):

ArrayList<Card> pack = new ArrayList<Card>();

for (Card.Value value : Card.Value.values()) {
    for (Card.Suit suit : Card.Suit.values()) {
        pack.add(new Card(suit, value));
    }
}

//finally, shuffle it:
Collections.shuffle(pack);