下面的程序得到一个无序的ArrayList,但是怎样把它转化成数组呢?谢谢。
或者直接得到一个无序的数组也可以。谢谢了。import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class RandomizeArray { /**
* @param args
*/
public static void main(String[] args) {
int[] sorted = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // 将数组内容放入 ArrayList
ArrayList sortedList = new ArrayList();
for (int i = 0; i < sorted.length; i++) {
sortedList.add(new Integer(sorted[i]));
} // 使用随机数
Random rnd = new Random(System.currentTimeMillis());
ArrayList randomizedList = new ArrayList();
for (int i = 0; i < sorted.length; i++) { // 随机从 sortedList 中抽取一个元素放入 randomizedList
int index = rnd.nextInt(sorted.length - i);
randomizedList.add(sortedList.get(index)); // 从 randomizedList 中去掉被抽取的元素,以免下次再被抽到
sortedList.remove(index);
} System.out.println(randomizedList);
}}
或者直接得到一个无序的数组也可以。谢谢了。import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class RandomizeArray { /**
* @param args
*/
public static void main(String[] args) {
int[] sorted = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // 将数组内容放入 ArrayList
ArrayList sortedList = new ArrayList();
for (int i = 0; i < sorted.length; i++) {
sortedList.add(new Integer(sorted[i]));
} // 使用随机数
Random rnd = new Random(System.currentTimeMillis());
ArrayList randomizedList = new ArrayList();
for (int i = 0; i < sorted.length; i++) { // 随机从 sortedList 中抽取一个元素放入 randomizedList
int index = rnd.nextInt(sorted.length - i);
randomizedList.add(sortedList.get(index)); // 从 randomizedList 中去掉被抽取的元素,以免下次再被抽到
sortedList.remove(index);
} System.out.println(randomizedList);
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货