日期:2014-05-20 浏览次数:20706 次
import java.util.Random; public class Test { public static void main(String[] args) { String s = "hello"; char[] cs = s.toCharArray(); charRandom(cs); System.out.println(String.valueOf(cs)); } public static void charRandom(char[] c) { int al = c.length; Random ran = new Random(); for(int i = 0; i < 100; i++) { int ranInt_1 = ran.nextInt(al); int ranInt_2 = ran.nextInt(al); if(ranInt_1 != ranInt_2) { char temp = 0; temp = c[ranInt_1]; c[ranInt_1] = c[ranInt_2]; c[ranInt_2] = temp; } } } }