Big Java 4

Chapter 7 – Arrays and Array Lists

Chapter Goals

Arrays

Arrays

Arrays

Use [] to access an element:
values[2] = 29.95;

Arrays

Declaring Arrays

Declaring Arrays

Syntax 7.1 Arrays

Arrays

Self Check 7.1

What elements does the values array contain after the following statements?
double[] values = new double[10];
for (int i = 0; i < values.length; i++) values[i] = i * i;

Self Check 7.2

What do the following program segments print? Or, if there is an error, describe the error and specify whether it is detected at compile-time or at run-time.
  1. double[] a = new double[10];
    System.out.println(a[0]);
  2. double[] b = new double[10];
    System.out.println(b[10]);
  3. double[] c;
    System.out.println(c[0]);

Make Parallel Arrays into Arrays of Objects

// Don't do this
int[] accountNumbers;
double[] balances;

Make Parallel Arrays into Arrays of Objects

Avoid parallel arrays by changing them into arrays of objects:
BankAccount[] accounts;

Array Lists

Adding Elements

To add an object to the end of the array list, use the add method:
names.add("Emily"); 
names.add("Bob"); 
names.add("Cindy"); 

Retrieving Array List Elements

Setting Elements

Removing Elements

To remove an element at an index, use the remove method:
names.remove(1);

Adding and Removing Elements

names.add("Emily"); 
names.add("Bob");
names.add("Cindy");
names.set(2, "Carolyn"); 
names.add(1, "Ann"); 
names.remove(1); 

Working with Array Lists

ArrayList<String> names =
   new ArrayList<String>();
Constructs an empty array list that can hold strings.
names.add("Ann");
names.add("Cindy");
Adds elements to the end.
System.out.println(names);
Prints [Ann, Cindy].
names.add(1, "Bob");
Inserts an element at index 1. names is now [Ann, Bob, Cindy].
names.remove(0);
Removes the element at index 0. names is now [Bob, Cindy].
names.set(0, "Bill");
Replaces an element with a different value. names is now [Bill, Cindy].
String name = names.get(i);
Gets an element.
String last =
   names.get(names.size() - 1);
Gets the last element.
ArrayList<Integer> squares =
   new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
{
   squares.add(i * i);
}
Constructs an array list holding the first ten squares.

Syntax 7.2 Array Lists

Array Lists

ch07/arraylist/ArrayListTester.java

Your browser does not support the <object> tag.

ch07/arraylist/BankAccount.java

Your browser does not support the <object> tag. Program Run:

Self Check 7.3

How do you construct an array of 10 strings? An array list of strings?

Self Check 7.4

What is the content of names after the following statements?
ArrayList<String> names = new ArrayList<String>();
names.add("A");
names.add(0, "B");
names.add("C");
names.remove(1);

Wrapper Classes

Wrapper Classes

There are wrapper classes for all eight primitive types:

Auto-boxing

Auto-boxing and Array Lists

Self Check 7.5

What is the difference between the types double and Double?

Self Check 7.6

Suppose values is an ArrayList<Double> of size > 0. How do you increment the element with index 0?

The Enhanced for Loop

The Enhanced for Loop

The Enhanced for Loop

Syntax 7.3 The for each Loop

Self Check 7.7

Write a for each loop that prints all elements in the array values.

Self Check 7.8

What does this for each loop do?
int counter = 0; 
for (BankAccount a : accounts) 
{ 
   if (a.getBalance() == 0) { counter++; } 
}

Partially Filled Arrays

Partially Filled Arrays

Partially Filled Arrays

Self Check 7.9

Write a loop to print the elements of the partially filled array values in reverse order, starting with the last element.

Self Check 7.10

How do you remove the last element of the partially filled array values?

Self Check 7.11

Why would a programmer use a partially filled array of numbers instead of an array list?

Common Array Algorithm: Filling

Common Array Algorithm: Computing Sum and Average

Common Array Algorithm: Counting Matches

Common Array Algorithm: Finding the Maximum or Minimum

Common Array Algorithm: Searching for a Value

Common Array Algorithm: Locating the Position of an Element

Common Array Algorithm: Removing an Element

Common Array Algorithm: Removing an Element

Animation 7.1: Removing from an Array

Link to Flash animation

Common Array Algorithm: Inserting an Element

Common Array Algorithm: Inserting an Element

Animation 7.2: Inserting into an Array

Link to Flash animation

Common Array Algorithm: Copying an Array

Common Array Algorithm: Growing an Array

Common Array Algorithm: Growing an Array

Common Array Algorithm: Printing Element Separators

ch07/bank/Bank.java

Your browser does not support the <object> tag.

ch07/bank/BankTester.java

Your browser does not support the <object> tag. Program Run:

Self Check 7.12

What does the find method do if there are two bank accounts with a matching account number?

Self Check 7.13

Would it be possible to use a for each loop in the getMaximum method?

Self Check 7.14

When printing separators, we skipped the separator before the initial element. Rewrite the loop so that the separator is printed after each element, except for the last element.

Self Check 7.15

The following replacement has been suggested for the algorithm that prints element separators:
System.out.print(names.get(0)); 
for (int i = 1; i < names.size(); i++)
   System.out.print(" | " + names.get(i));
What is problematic about this suggestion?

Regression Testing

ch07/regression/BankTester.java

Your browser does not support the <object> tag.

Regression Testing: Input/Output Redirection

Self Check 7.16

Suppose you modified the code for a method. Why do you want to repeat tests that already passed with the previous version of the code?

Self Check 7.17

Suppose a customer of your program finds an error. What action should you take beyond fixing the error?

Self Check 7.18

Why doesn't the BankTester program contain prompts for the inputs?

Therac-25 Facility

Two-Dimensional Arrays

Traversing Two-Dimensional Arrays

ch07/twodim/TicTacToe.java

Your browser does not support the <object> tag.

ch07/twodim/TicTacToeRunner.java

Your browser does not support the <object> tag. Program Run:

Self Check 7.19

How do you declare and initialize a 4-by-4 array of integers?

Self Check 7.20

How do you count the number of spaces in the tic-tac-toe board?