Arrays

Arrays #

Java arrays are used to store multiple values in one variable. Array is created by typing type of the values and then by typing square brackets.

The following code declares an array with 3 values.

String[] animals = {"Cat", "Dog", "Bird"};

Inorder to create an array of integers the int type can be used.

int[] someNumbers = {15, 5, 7};

In order to print the first and second values the following code can be used.

String[] animals = {"Cat", "Dog", "Bird"};
System.out.println(animals[0]);
System.out.println(animals[1]);
// The output will be Cat and Dog.

Note that the array starts with 0 index.