【java】怎么随机的遍历一个数组内的所有元素?

比如:一个数组有‘a’,'b','c','d',四个元素,怎么才能做到打乱顺序全部输出他们呢?
打乱顺序当然不是人为的,是随机打乱的

可以随机产生0-3的数字,然后输出。如果每个字母输出一次,可以借助HashMap来产生数字
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-11-01
Random rd=new Random()
System.out.println(string[rd.nextInt(3)];
第2个回答  2010-11-01
import java.util.Random;
import java.util.Vector;

public class test{

/**
* @param args
*/
public static void main(String[] args) {
String[] strs = { "a", "b", "c", "d"};
int strsLength = strs.length;
Vector<Integer> v = new Vector<Integer>();
Random r = new Random();
boolean flag = true;

while (flag) {
int i = r.nextInt(strsLength);
if (!v.contains(i))
v.add(i);
if (v.size() == strsLength)
flag = false;
}
for (int i = 0; i < strsLength; i++) {
System.out.println(strs[v.get(i)]);
}

}
}本回答被提问者采纳