Skip to content

Instantly share code, notes, and snippets.

@L3au
Created March 17, 2012 16:25
Show Gist options
  • Save L3au/2061738 to your computer and use it in GitHub Desktop.
Save L3au/2061738 to your computer and use it in GitHub Desktop.
从(Min,Max)的范围内产生Num个随机数,而且还不能重复的。。(ArrayList,Iterator)
import java.util.ArrayList;
import java.util.Iterator;
public class RandomTrial{
public static void main(String[] args){
String [] s = {"one","two","three","four","five","six","seven","eight","nine","ten"};
ArrayList<Integer> al = new ArrayList<Integer>();
while(al.size() < 3){
int num = (int)(Math.random() * 10);
if (!al.contains(num)) {
al.add(num);
}
}
Iterator it = al.iterator();
while(it.hasNext()){
int i = (int)it.next();
System.out.print(s[i]+" ");
}
/*
int [] a = new int[3];
for (int i = 0; i < al.size(); i++) {
a[i] = al.get(i);
}
for (int i : a) {System.out.print(s[i]+" ");
System.out.print(s[i]+" ");
}
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment