How to create and initialize array in java?

Java example to create and initialize array in java

public class ArraysDemo {

	public static void main(String[] args) {

		//Creating an array

		int a[]=new int[5];

		//Adding elements to array by accessing its index

		a[0]=1;
		a[1]=2;
		a[2]=3;
		a[3]=4;
		a[4]=5;
		a[5]=6;

		//Printing the elements in array
		// length variable is used to know the size of the array
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}

	}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *