- Sections 7.5 - 7.6.5

- Array length = maximum number of elements in array
- Usually, array is partially filled
- Need companion variable to keep track of current size
- Uniform naming convention:
final int VALUES_LENGTH = 100; double[] values = new double[VALUES_LENGTH]; int valuesSize = 0;

- Update
`valuesSize`as array is filled:values[valuesSize] = x; valuesSize++;

- Example: Read numbers into a partially filled array:
int valuesSize = 0; Scanner in = new Scanner(System.in); while (in.hasNextDouble()) { if (valuesSize < values.length) { values[valuesSize] = in.nextDouble(); valuesSize++; } }

- To process the gathered array elements, use the companion variable, not
the array length:
`for (int i = 0; i < valuesSize; i++) { System.out.println(values[i]); }`

**Answer:**for (int i = valuesSize - 1; i >= 0; i--) System.out.println(values[i]);

`values.length--;`

`valuesSize--;`

`values.remove(values.size() - 1);`

`values[valuesSize] = 0;`

- Because arrays are always more efficient than array lists.
- Because you need to use wrapper objects in an
`ArrayList<Double>`, which is less efficient. - Because array lists automatically adjust their capacity.
- To avoid roundoff errors.

- Fill an array with zeroes:
for (int i = 0; i < values.length; i++) { values[i] = 0; }

- Fill an array list with squares (0, 1, 4, 9, 16, ...):
for (int i = 0; i < values.size(); i++) { values.set(i, i * i); }

- To compute the sum of all elements, keep a running total:
double total = 0; for (double element : values) { total = total + element; }

- To obtain the average, divide by the number of elements:
double average = total / values.size(); // For an array list

- Be sure to check that the size is not zero

What needs to be done to adapt this algorithm to a completely filled array?

- Nothing
- Change the enhanced for loop to a regular for loop
- Change
`values.size()`

to`valuesSize`

- Change
`values.size()`

to`values.length`

- Check all elements and count the matches until you reach the end
- Example: Count the number of accounts whose balance is at least as much
as a given threshold:
public class Bank { private ArrayList<BankAccount> accounts; public int count(double atLeast) { int matches = 0; for (BankAccount account : accounts) { if (account.getBalance() >= atLeast) matches++; // Found a match } return matches; } . . . }

- Initialize a candidate with the starting element
- Compare candidate with remaining elements
- Update it if you find a larger or smaller value
- Example: Find the account with the largest balance in the bank:
BankAccount largestYet = accounts.get(0); for (int i = 1; i < accounts.size(); i++) { BankAccount a = accounts.get(i); if (a.getBalance() > largestYet.getBalance()) largestYet = a; } return largestYet;

- Works only if there is at least one element in the array list — if list
is empty, return
`null`:if (accounts.size() == 0) return null; BankAccount largestYet = accounts.get(0); . . .

Consider the following modification of the algorithm to find a maximum in an
array of `double`

values.

double largestYet = 0; for (double value : values) { if (value > largestYet) largestYet = value; } return largestYet;

Which of the following statements is correct?

- The algorithm works correctly for all arrays
- The algorithm doesn't work for an empty array
- The algorithm doesn't work for an array of length 1
- The algorithm doesnt' work for an array of negative numbers

- Check all elements until you have found a match
- Example: Determine whether there is a bank account with a particular
account number in the bank:
public class Bank { public BankAccount find(int accountNumber) { for (BankAccount account : accounts) { if (account.getAccountNumber() == accountNumber) // Found a match return account; } return null; // No match in the entire array list } . . . }

- This search process is called a
**linear search**

Consider this modification of the linear search algorithm for an array of
`double`

values.

public int find(double[] values, double number) { for (int i = 0; i < values.length; i++) { if (values[i] == number) return i; } return -1; }

Which of the following statements is true about this algorithm?

- In the last line, it should
`return null`

- The algorithm doesn't work if all values are negative
- The algorithm doesn't return the number that is being searched
- The
`for`

loop is always traversed`values.length`

times