*
Title:
* Generate 6 random integers between 1 and 33, add to the set, and traverse the set
*
*
* Idea:
* 1. Need to be purely 6 numbers, create a set, <Integer> use numbers
* 2. To generate random numbers, you need to use Random
* 3. It takes 6 times to generate 6 random numbers, for loop
* 4. Call r.nextInt (int n) in the loop, the parameter is 33,0 ~ 32; overall +1 Only 33
* 5. Add the number to the collection: add
* 6 traverse the collection: for, size, get
import java.util.ArrayList;
import java,util.Random;
public class Text01ArrayListRandom {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<>();
Random r=new Random();
for(int i=0;i<6;i++) {//利用for遍历 随机得到6个数, 然后再下面循环遍历出来
int num=r.nextInt(33)+1;
list.add(num);
}
for(int i=0;i<list.size();i++) {//遍历出集合
System.out.println(list.get(i));
}
}
}
result:22
18
33
23
19
7