一个数组比如String [] s={"sdf","efd","wefd","efdd"};
我想随机从里面取三个值,最好给另外一个数组,请问通过程序如何做啊,谢谢
------解决方案--------------------------------------------------------
额。。。。我想挣分的,这下没机会了。
------解决方案--------------------------------------------------------
顶一下吧!计算出数组的长度,产生不大于下标的随机数,用下标取
------解决方案--------------------------------------------------------
- Java code
String [] s = {"sdf", "efd", "wefd", "efdd"}; final String[] target = new String[3]; final Random random = new Random(); for(int i = 0 ; i < 3; i ++) { int index = random.nextInt(s.length); target[i] = s[index]; } System.out.println(Arrays.toString(target));
------解决方案--------------------------------------------------------
用split数组分割法,保存到数组中,然后random,在取值