Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type with square brackets.
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};
Access the Elements of an Array
You can access an array element by referring to the index number.
System.out.println(cars[0]);
Change an Array Element
To change the value of a specific element, refer to the index number:
cars[0] = "Opel";
Array Length
To find out how many elements an array has, use the length property:
System.out.println(cars.length);
Loop Through an Array
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.
Loop Through an Array with For-Each
There is also a "for-each" loop, which is used exclusively to loop through elements in arrays