Created
March 17, 2012 16:25
-
-
Save L3au/2061738 to your computer and use it in GitHub Desktop.
从(Min,Max)的范围内产生Num个随机数,而且还不能重复的。。(ArrayList,Iterator)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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