Java 如何“洗牌”一个数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18470094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:53:21  来源:igfitidea点击:

How to "shuffle" an array?

javaarraysswapshuffle

提问by user2690972

I am having a tough time trying to create a "shuffleDeck()" method.

我在尝试创建“shuffleDeck()”方法时遇到了困难。

What I am trying to do is create a method that will take an array parameter (which will be the deck of cards) shuffle the cards, and return the shuffled array list.

我想要做的是创建一个方法,该方法将采用数组参数(将是一副牌)洗牌,并返回洗牌的数组列表。

This is the code:

这是代码:

class Card
{
    int value;
    String suit;
    String name;

    public String toString()
    {
        return (name + " of " + suit);
    }
}

public class PickACard
{
    public static void main( String[] args)
    {   
        Card[] deck = buildDeck();
        // display Deck(deck); 

        int chosen = (int)(Math.random()* deck.length);
        Card picked = deck[chosen];

        System.out.println("You picked a " + picked + " out of the deck.");
        System.out.println("In BlackHyman your card is worth " + picked.value + " points.");

    }

    public static Card[] buildDeck()
    {
        String[] suits = {"clubs", "diamonds", "hearts", "spades" };
        String[] names = {"ZERO", "ONE", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "Hyman", "Queen", "King", "Ace" };

        int i = 0;
        Card[] deck = new Card[52];

        for ( String s: suits )
        {   
            for ( int v = 2; v<=14; v++)
            {
                Card c = new Card();
                c.suit = s;
                c.name = names[v];
                if ( v == 14)
                    c.value = 11;
                else if ( v>10)
                    c.value = 10;
                else
                    c.value = v; 

                deck[i] = c;
                i++;
            }
        }
        return deck; 
    }

    public static String[] shuffleDeck( Card[] deck) 
    {
        /** I have attempted to get two index numbers, and swap them. 
        I tried to figure out how to loop this so it kind of simulates "shuffling". 
        */
    }

    public static void displayDeck( Card[] deck)
    {
        for ( Card c: deck) 
        {   
            System.out.println(c.value + "\t" + c);
        }
    }
}

回答by rocketboy

How about:

怎么样:

List<Card> list =  Arrays.asList(deck);
Collections.shuffle(list);

Or one-liner:

或单线:

Collections.shuffle(Arrays.asList(deck));

回答by Julien

I see two ways to do it:

我看到了两种方法:

-> You can use a shuffle algorithm like the Fisher-Yates shufflealgorithm if you want to implement yourself the method.

->如果你想自己实现这个方法,你可以使用像Fisher-Yatesshuffle 算法这样的shuffle算法。

-> You can use the shuffle method from Collections

-> 您可以使用Collections 中shuffle 方法

回答by supertopi

If this is for a school project (as I think it is), you might not be allowed to use built-in functions such as Collections::shuffle(). If this is the case, then you must try to simulate randomness (which in programming can be surprisingly hard).

如果这是用于学校项目(正如我认为的那样),则可能不允许您使用诸如 Collections::shuffle() 之类的内置函数。如果是这种情况,那么您必须尝试模拟随机性(这在编程中可能非常困难)。

The most common way to create a sense of randomness is to use an RNG (random number generator). As you said

创建随机感的最常见方法是使用RNG(随机数生成器)。如你所说

I have attempted to get two index numbers, and swap them.

我试图获得两个索引号,然后交换它们。

Correct. One way to shuffle is to pick one card at a time and randomly select another card to swap the position with.

正确的。一种洗牌方法是一次选择一张牌,然后随机选择另一张牌来交换位置。

  • You know the deck always has 52 cards.
  • You have a random generator to select a random index.
  • You have a programming language with loop-structures.
  • 你知道一副牌总是有 52 张牌。
  • 您有一个随机生成器来选择一个随机索引。
  • 你有一种带有循环结构的编程语言。

With these tools you can implement your own shuffle-function quite easily.

使用这些工具,您可以很容易地实现自己的随机播放功能。

回答by Kevin Cruijssen

One way is to convert the array to a list, and use java.util.Collections.shuffle(array)to shuffle it:

一种方法是将数组转换为列表,并使用java.util.Collections.shuffle(array)shuffle 它:

Card[] deck = ...;
List<Card> list = Arrays.asList(deck);
Collections.shuffle(list);

If you do still need an array instead of a List, you can add:

如果你仍然需要一个数组而不是一个列表,你可以添加:

list.toArray(deck);


Here is a TIO (Try-it-online) link to see the array to list conversion and shuffling in action.

这是一个 TIO (Try-it-online) 链接,用于查看列出转换和改组操作的数组。

Code of the TIO copied below as reference:

下面复制的 TIO 代码作为参考:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class M{
  public static void main(String[] a){
    // Original array
    Integer[] array = new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    System.out.println("before: " + Arrays.toString(array));

    // Convert array to list
    List<Integer> list = Arrays.asList(array);
    // And shuffle that list
    Collections.shuffle(list);
    System.out.println("after as list: " + list);

    // (Optional) then convert the list back to an array,
    // and save it in its initial variable (`array` in this case)
    list.toArray(array);
    System.out.println("after as array: " + Arrays.toString(array));
  }
}