CS 46A - Lecture 17

Cover page image

Pre-class reading

Arrays

Arrays

Arrays

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

Arrays

Declaring Arrays

Declaring Arrays

Syntax 7.1 Arrays

Arrays

Lecture 17 Clicker Question 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 + 1) * (i + 1);

  1. 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, but not 100
  2. 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
  3. 4, 9, 16, 25, 36, 49, 64, 81, 100, 121
  4. An exception has been thrown

Lecture 17 Clicker Question 2

What is true about the following code segments?

  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]);
  1. #1 and #2 throw an exception, #3 doesn't compile
  2. All three compile and throw an exception
  3. #1 prints 0, #2 and #3 don't compile
  4. #1 prints 0, #2 throws an exception, and #3 doesn't compile

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:

Lecture 17 Clicker Question 3

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);

  1. [A, B, C]
  2. [B, A, C]
  3. [B, C]
  4. Something else