how to initialize a dynamic array in java?


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

Updated on: 24-Feb-2020

404 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements