How to work with arrays

There are two ways to define an array:

  • type[] arrayName;
  • type arrayName[];

How to initialize:

  • arrayName = new type[length];

How to assign values to the elements:

  • type[0] = "value";
  • type[] arrayName = {value1, value2, value3, . . . };

To refer to an element in the array, use an index:

  • type[index] = value;

Looping on arrays:

double[] prices = {14.44, 50.44, 9.95};
for (double price : prices)
{
System.out.println(price);
}