how to initialize a dynamic array in java?

Java 8Object Oriented ProgrammingProgramming

Following program shows how to initialize an array declared earlier.

Example

public class Tester {
   int a[];
   public static void main(String[] args) {
      Tester tester = new Tester();
      tester.initialize();
   }
   private void initialize() {
      a = new int[3];
      a[0] = 0;
      a[1] = 1;
      a[2] = 2;
      for(int i=0; i< a.length ; i++) {
         System.out.print(a[i] +" ");
      }
   }
}

Output

0 1 2
raja
Published on 06-Feb-2018 11:46:39
Advertisements