从java中的字符串数组中选择一个随机项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21726033/
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-13 10:20:47  来源:igfitidea点击:

Picking a random item from an array of strings in java

javaarraysrandom

提问by user3266734

I have some arrays containing Strings and I would like to select randomly an item from each array. How can I accomplish this?

我有一些包含字符串的数组,我想从每个数组中随机选择一个项目。我怎样才能做到这一点?

Here are my arrays:

这是我的数组:

static final String[] conjunction = {"and", "or", "but", "because"};

static final String[] proper_noun = {"Fred", "Jane", "Richard Nixon", "Miss America"};

static final String[] common_noun = {"man", "woman", "fish", "elephant", "unicorn"};

static final String[] determiner = {"a", "the", "every", "some"};

static final String[] adjective = {"big", "tiny", "pretty", "bald"};

static final String[] intransitive_verb = {"runs", "jumps", "talks", "sleeps"};

static final String[] transitive_verb = {"loves", "hates", "sees", "knows", "looks for", "finds"};

回答by Nigel Tufnel

Use the Random.nextInt(int)method:

使用Random.nextInt(int)方法:

final String[] proper_noun = {"Fred", "Jane", "Richard Nixon", "Miss America"};
Random random = new Random();
int index = random.nextInt(proper_noun.length);
System.out.println(proper_noun[index]);

This code is not completely safe: one time out of four it'll choose Richard Nixon.

这段代码不是完全安全的:四分之一它会选择理查德尼克松。

To quote a documentation Random.nextInt(int):

引用文档Random.nextInt(int)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

返回一个伪随机的、均匀分布在 0(含)和指定值(不含)之间的 int 值

In your case passing an array length to the nextIntwill do the trick - you'll get the random array index in the range [0; your_array.length)

在您的情况下,将数组长度传递给nextInt将会起作用 - 您将获得范围内的随机数组索引[0; your_array.length)

回答by Dropout

If you want to cycle through your arrays you should put them into an array. Otherwise you need to do the random pick for each one separately.

如果要循环遍历数组,则应将它们放入数组中。否则,您需要分别对每一个进行随机选择。

// I will use a list for the example
List<String[]> arrayList = new ArrayList<>();
arrayList.add(conjunction);
arrayList.add(proper_noun);
arrayList.add(common_noun);
// and so on..

// then for each of the arrays do something (pick a random element from it)
Random random = new Random();
for(Array[] currentArray : arrayList){
    String chosenString = currentArray[random.nextInt(currentArray.lenght)];
    System.out.println(chosenString);
}

回答by user902383

if you use Listinstead of arrays you can create simple generic method which get you random element from any list:

如果您使用List而不是数组,您可以创建简单的通用方法,从任何列表中获取随机元素:

public static <T> T getRandom(List<T> list)
{
Random random = new Random();
return list.get(random.nextInt(list.size()));
}

if you want to stay with arrays, you can still have your generic method, but it will looks bit different

如果你想继续使用数组,你仍然可以使用你的泛型方法,但它看起来会有点不同

public static <T> T   getRandom(T[] list)
{
    Random random = new Random();
    return list[random.nextInt(list.length)];

}