使用带有随机数的两个数组打印Arraylist

时间:2015-04-02 15:08:18

标签: java arrays arraylist

有人告诉我出了什么问题。我想生成一个带有两个带有radom数的动态数组的Arraylist。例如,我想打印7行和5列的矩阵。而且我的矩阵也不会有重复。

ArrayList<int[]> lottoraws= new ArrayList<int[]>();

    int[] a = new int[5];
    int[] b = new int[7];

    for (int i = 0; i < 7; i++) {
        int rand = (int) (Math.random() * 35 + 1);
        b[i] = rand;

        for (int j = 0; j < 5; j++) {
            int rand2 = (int) (Math.random() * 35 + 1);
            a[j] = rand2;
    }
    }
    lottoraws.add(a);
    lottoraws.add(b);

    System.out.println("The players row: \n" + Arrays.toString(lottoraws.get(0)));

2 个答案:

答案 0 :(得分:0)

那样的东西?

    int[][] a = new int[7][5];
    for (int i = 0; i < a.length; i++) {
        for(int j = 0; j < a[0].length ; j++){
            a[i][j] = (int) (Math.random() * 35 + 1);
        }
    }

答案 1 :(得分:0)

请尝试以下操作,您可以根据需要更改随机方案。

    ArrayList<int[]> lottoraws = new ArrayList<int[]>();
    int[] a = {};

    Random random = new Random();
    for (int i = 0; i < 7; i++) {

        a = new int[5];

        for (int j = 0; j < 5; j++) {
            int rand2 = (int) (random.nextInt(200));
            a[j] = rand2;
        }
        lottoraws.add(a);
    }

    for(int k=0; k< lottoraws.size(); k++){
        System.out.println(Arrays.toString(lottoraws.get(k)));
    }
}