如何把数组里的数随机打印出来?

tanghaijie595 2006-07-31 05:57:48
比如有这样一个数组,int[] x ={1,2,3,4,5};

如何把这五个数随机打印出来呢?
我想过可能是用Random()吧?不过我没有实现出来.
...全文
424 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanghaijie595 2006-08-02
  • 打赏
  • 举报
回复
对,你说的对,散列表并不是真正的随机
你刚才方法我又试了下,竟然又对了,也许是我那天不小心多打了个什么字母吧...
tanghaijie595 2006-08-02
  • 打赏
  • 举报
回复
嗯.
今天我又想了一下,确实是楼上说的那位最好.
可惜分给完了
hmily_ysp 2006-08-01
  • 打赏
  • 举报
回复
参考下
public class ArrayList {
public static void main(String[] args) {
int[] x ={1,2,3,4,5};
int t =(int)( Math.random() * x.length + 1);
System.out.println(t);
}
}
程序獵人 2006-08-01
  • 打赏
  • 举报
回复
嘻嘻哈哈的算法不如我下面的算法省内存,不过两种算法都不如我上面贴的那个效率高。

int[] a = new int[1000000];

for (int i = 0; i < a.length; i++) {
a[i] = i;
}

final int offset = 5;
long[] flag = new long[(a.length >> offset) + 1];
int printedCount = 0;
Random rand = new Random();
long time = System.currentTimeMillis();
for (int i = 0; i < a.length; i++) {
int index;
int flagIndex;
int flagValue;
do {
index = rand.nextInt(a.length);
flagIndex = index >> offset;
flagValue = 1 << (index - (flagIndex << offset));
} while ((flag[flagIndex] & flagValue) != 0);
System.out.println(a[index]);
printedCount++;
flag[flagIndex] |= flagValue;
}

System.out.println("Time: " + (System.currentTimeMillis() - time));
程序獵人 2006-08-01
  • 打赏
  • 举报
回复
int[] a = new int[1000000];

for (int i = 0; i < a.length; i++) {
a[i] = i;
}

int[] b = new int[a.length];
System.arraycopy(a, 0, b, 0, a.length);
Random rand = new Random();
for (int i = b.length - 1; i > 1; i--) {
int index = rand.nextInt(i);
int t = b[i];
b[i] = b[index];
b[index] = t;
}

for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}

lookhang 2006-08-01
  • 打赏
  • 举报
回复
看看这个··已经测试 ··
import java.util.*;

public class RandomPrint {
private static Random rand = new Random();
public static final int[] x = {1,2,3,4,5};
public static void main(String[] args) {
boolean[] picked = new boolean[x.length];
for(int i = 0; i < x.length; i++) {
int t;
do
t = rand.nextInt(x.length);
while(picked[t]);
System.out.println(x[t]);
picked[t] = true;
}

}
}
程序獵人 2006-08-01
  • 打赏
  • 举报
回复
其实,我觉得Dan1980的方法最好,最简单,而且对要求实现的也好。
polarman 2006-08-01
  • 打赏
  • 举报
回复
生成五个0~1的随机数,并保证没有重复(一般不会重复),假设如下(括号内的数字是这个数产生的顺序)
(1)0.1 (2)0.3 (3)0.5 (4)0.2 (5)0.4

将他们排序
(1)0.1 (4)0.2 (2)0.3 (5)0.4 (3)0.5

然后按1 4 2 5 3的顺序打印
tanghaijie595 2006-08-01
  • 打赏
  • 举报
回复
多谢大家,通过这次事件,我觉得自己还是比较笨呀,这么多的方法我竟然一个也想不到.
个人认为最好的方法是"staticvoid()"的.
当然其他的方案也都不错,不过有的有点复杂对于我来说,呵呵.
staticvoid 2006-07-31
  • 打赏
  • 举报
回复
最简单的方法:利用HashSet,因为它本来就采用散列序列:
import java.util.*;
public class PtArray {
static void printArray(int[]a){
Set set=new HashSet(a.length);
for(int i=0;i<a.length;i++)
set.add(new Integer(a[i]));
System.out.println(set);
}
public static void main(String[] args) {
printArray(new int[]{1,2,3,4,5,6,7,8,9});
}
}
staticvoid 2006-07-31
  • 打赏
  • 举报
回复
最简单的方法:利用HashSet,因为它本来就采用散列序列:
import java.util.*;
public class PtArray {
static Random rand=new Random();
static void printArray(int[]a){
Set set=new HashSet(a.length);
for(int i=0;i<a.length;i++)
set.add(new Integer(a[i]));
System.out.println(set);
}
public static void main(String[] args) {
printArray(new int[]{1,2,3,4,5,6,7,8,9});
}
}
Dan1980 2006-07-31
  • 打赏
  • 举报
回复
把数组元素放到一个List里面,用Collections.shuffle()一下,再打印出来就可以了。
W_Cracker 2006-07-31
  • 打赏
  • 举报
回复
//给你个洗扑克牌的代码,自已写的,照这个应该能写出你的那个!!
public class Pork
{
private String[] strArr1={"红桃","黑桃","方块","梅花"};
private String[] strArr2={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
private String[] strArr3=new String[52]; //用于存放生成牌的数组;

public Pork()
{

}
public static void main(String[] args)
{
Pork pork = new Pork();
pork.run();
}

public void run()
{
for(int i=0;i<strArr1.length;i++)
{
for(int j=0;j<strArr2.length;j++)
{
String str = new String(strArr1[i]+strArr2[j]);
Random random = new Random();
//------------------------------- 以下是洗牌的过程 ----------------------------//
retry:
while(true)
{
int k = random.nextInt(52); //从0-51之间的随机数;
if(strArr3[k]==null) //如果数组的随机数位不为空,则将数据插入;
{
strArr3[k]=str;
break retry;
}
}
}
}
for(int i=0;i<52;i++) //将结果输出;
{
System.out.println(strArr3[i]);
}
}
}
W_Cracker 2006-07-31
  • 打赏
  • 举报
回复
再写一个同样长度的数组,用随机数向里插值,并判断当前位置是否有值
如果有就继续取,插。插完数据以后,将这个新的数据打出来就可以了。
maquan 2006-07-31
  • 打赏
  • 举报
回复
以下是前两天我在另一个帖子里给出的程序,承蒙楼主慷慨,这个回帖赚了 100 分 :D
现在一稿两投,再送给你吧

http://community.csdn.net/Expert/topic/4914/4914241.xml?temp=.6360437

List list = new ArrayList();
for (int i=0; i<52; i++)
list.add(new Integer(i+1));
Random random = new Random();
while (list.size() > 0) {
int rand = random.nextInt(list.size());
rand = (Integer)list.remove(rand);
// 这个就是你要的了
System.out.println(rand);
}
tanghaijie595 2006-07-31
  • 打赏
  • 举报
回复
回答楼上,这样做完全无法做到把把数组里的每个元素都不少不多的打出来
很可能某个元素多打或是某个元素没打出来
yczz 2006-07-31
  • 打赏
  • 举报
回复
用Random生成个0-4的随机数作下标

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧