Java Collection How to - Generate random array and print array into 10 lines of 10








Question

We would like to know how to generate random array and print array into 10 lines of 10.

Answer

import java.util.Random;
//  ww  w .j  a  va  2  s.co  m
public class Main {

  public static void main(String... args) {

    Random rand = new Random();
    int[] a = new int[100];
    for (int i = 0; i < 100; i++) {
      a[i] = rand.nextInt(100);
      System.out.print(a[i] + ", ");
      if ((i + 1) % 10 == 0) {
        System.out.println();
      }
    }
  }
}

The code above generates the following result.