posts - 23, comments - 6, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

java固定数组如何随机输出

Posted on 2006-11-29 16:01 小傻 阅读(453) 评论(0)  编辑  收藏 所属分类: jsp

import java.util.Random;
public class test2 {
 public static void main(String args[]) {
  RandomTest();
 }
 
 // 数组自由排列
 public static void RandomTest(){
  Random ran = new Random();
  int a[] = { 1, 2, 3, 4, 5 ,6,7,8,9};
  int b[] = new int[a.length];
  int w = a.length;
  int x = 0;
  int y;
  
  for (int i = 0; i < a.length; i++) {  
   y = 0;
   x = Math.abs(ran.nextInt()) % w;
   // 把随机选中的数,提出来,然后省下数重新排列数组
   for (int j = 0; j < w; j++) {
    if (x == j) {
     b[i] = a[x];
     continue;15:59:4715:59:582006年11月29日
    }
    a[y] = a[j];
    y++;
   }
   w--;
   System.out.print("~" + b[i] + "~");
  }
 }
}


只有注册用户登录后才能发表评论。


网站导航: