发下我的答案把
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Random;
- public class RandomDemo {
- public static void main(String[] args) {
- // 生成10 个1-100个不重复的随机数保存在ArrayList集合里面
- ArrayList al = new ArrayList();
- Random r = new Random();
- while (al.size() < 10) {
- int i = r.nextInt(100) + 1;
- if (!al.contains(i)) {
- al.add(i);
- }
- }
- Iterator i = al.iterator();
- while (i.hasNext()) {
- System.out.print(i.next() + ", ");// 39, 53, 52, 86, 96, 79, 37, 43,94, 38,
- }
- }
- }
复制代码 |