Using for Loops with Arrays - Java Language Basics

Java examples for Language Basics:for

Introduction

One way to process an array is with a for loop.

For example, here's a for loop that creates an array of 100 random numbers, with values from 1 to 100:

Demo Code

public class Main {

  public static void main(String[] args) {
    int[] numbers = new int[100];
    for (int i = 0; i < 100; i++)
      numbers[i] = (int) (Math.random() * 100) + 1;
  }//from  w  w w  .ja v  a 2  s  . c  o  m

}

Related Tutorials