Arrays in Java

So there’s something called Array in Java. In an array, you can store fixed-size sequential collection of elements of the same type.

The simplest way to create an array is using the following code:

int[] orray = {1,2,3,4,5,6};
System.out.println(orray[0]);

This code declares and creates an array called orray of size 6 starting from index 0 to index 5. And on the next line we use print statement to print the element at position 0 in the array (which is 1).

The coolest part about arrays is that we can run a loop and print (or do something with) all the elements in the array as follows:

class hello {
	public static void main (String args[]) {
		int[] orray = {1,2,3,4,5,6};

		for(int i=0; i<orray.length; i++) {
			System.out.println(orray[i] + " ");
		}
	}
}

This should print numbers from 1 to 6 (elements in the array), each on it’s own line. We can do so much more with arrays but that’s all you need to know for now!