CS 46A - Lecture 19

Cover page image

Pre-class reading

Wrapper Classes

Wrapper Classes

There are wrapper classes for all eight primitive types:


Auto-boxing and Array Lists

Lecture 18 Clicker Question 1

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

  1. values.set(0, values.get(0) + 1);
  2. values.get(0)++;
  3. values.set(0, values.get(0)++);
  4. values[0]++;

The Enhanced for Loop

The Enhanced for Loop

The Enhanced for Loop

Syntax 7.3 The for each Loop

Lecture 19 Clicker Question 2

What does this for each loop do?

for (double element : values) 
  1. It prints all elements, separated by commas
  2. It prints all elements, separated by commas, and an additional comma
  3. It prints all elements, followed by a comma
  4. Something else

Lecture 19 Clicker Question 3

What is true about this for each loop?

int counter = 0; 
for (BankAccount a : accounts) 
   if (a.getBalance() >= 0) { counter++; } 

  1. It counts how many accounts are overdrawn
  2. If accounts has length 0, counter is zero
  3. accounts must be an array list
  4. It gives the wrong answer if all balances are negative