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: 2020-02-24T11:16:20+05:30

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements