## Arrays

An array is a list of items of the same type in which each item is stored at a specific, numbered position. Arrays are indicated in Java using square brackets (`[]`).

Example 1: The following declaration creates a reference to an array called salary. The declaration states that salary will only contain double values. The second statement assigns a particular array of 50 double values to salary.
```   double[] salary;
salary = new double;
```
This could also be accomplished in a single statement:
`   double[] salary = new double; `

What is an array?

• A list of data items of the same type.
• The items are sometimes referred to as the elements of the array.
• The items are numbered. Each item is identified by its position (e.g., index or subscript).
• An array supports random access. The time to access the first item is the same as the time to access the fifteenth item or the hundredth item.

What additional properties do arrays in Java have?
• The index values start at 0.
• Use the `new` to allocate space for an array. (The declaration of an array variable does not allocate space for the array values, only for the reference to the array.)
• Alternatively, you can allocate space by giving an explicit list of values in braces.
• Arrays know how big they are (`arrayname.length`).
• Array variables can be reassigned to different arrays at any time.

Example 2: Arrays can be initialized to contain specific values:

`    int[] coinValues = {1, 5, 10, 25, 50};  // Initializer Lists `
The array `coinValues` contains 5 integers: the values 1, 5, 10, 25 and 50 respectively.

Example 3: Array values are accessed by giving the array name followed by the position of the item in brackets:
`    int x = coinValues;`
The variable `x` is one after this statement is executed.

Exercise 1: Let:
`   int [] numbers = {5, -30, 20, 1, 7, 0, 22};`
Write code segments using the array `numbers` to do the following:

• Print the array forwards.
• Print the array backwards.
• Print only the even elements.
• Sum the elements in the odd indices.
• Average all the elements.
• Find the maximum value in numbers.
• Find the minimum value in numbers.
Use `numbers.length` rather than explicitly giving the number `7` when you need the length of the array.

### Arrays as parameters

Arrays can be designated as parameters by giving the type followed by ``` []``` instead of just the type.

Example 4: The printArray method outputs the elements of the array numbers on one line separated blanks.
```   public static void printArray(int[] numbers) {
for (int k = 0; k < numbers.length; k++)
System.out.print(numbers[k] + " ");
System.out.println();
} ```