ArrayList in the JAVA generating random data 6

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43153854/article/details/102753854

Title :
generating a random number between 6 1-33, added to the collection, and through the collection.

Ideas :
1. 6 digits need to be stored, creating a set of
2 generates a random number, need to use the Random
3. 6 cycles used to generate random numbers 6, for loop
calls r.nextInt (int 4. The inner loop n-), the parameter is 33,0-32, 1-33 is integrally +1
5. among digital added to the collection, the Add
6. the traverse set: for, size, get

public class DemoArrayListRandom {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 6 ; i++) {
            int num = r.nextInt(32) + 1 ;
            list.add(num);
        }

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_43153854/article/details/102753854