编程语言
首页 > 编程语言> > JAVA8 从数组中随机一个元素

JAVA8 从数组中随机一个元素

作者:互联网

 

 

import java.util.Random;
@Test
public void randomList() {
    String[] li = {"中国","美国","英国","法国","德国","俄罗斯"};
    Random random = new Random();
    for (int j = 0; j < 5; j++) {
        String country = li[random.nextInt(li.length)];
        System.out.println("country = " + country);
    }
}

output =>
country = 法国
country = 中国
country = 俄罗斯
country = 中国
country = 中国

标签:random,String,country,Random,li,随机,数组,俄罗斯,JAVA8
来源: https://www.cnblogs.com/yan061/p/15978836.html