website/content/ta/spring2018/cpsc220/mar29.md
2020-01-15 21:51:49 -05:00

1.2 KiB

Lecture for March 29th

Enumerated Types

These represent a fixed set of constants and include all possible values within them.

Let's look at coins. On a daily basis in the US, we use the following coins:

  • Penny
  • Nickel
  • Dime
  • Quarter

Other examples include the days of the week, clothes sizes, etc.

Enum Syntax

Let's define an enum type

public enum Coin { PENNY, NICKEL, DIME, QUARTER}

Now declare and initialize a variable

Coin myCoin = Coin.PENNY

Arrays vs ArrayList

Arrays require you to say upfront, how many slots you need. ArrayLists are more flexible since you can change the length of the array during Runtime.

Arrays can store objects and primitives such as int, char, boolean, etc.

ArrayLists can only store objects.

How to declare an ArrayList

ArrayList<objectType> list = new ArrayList<objectType>();

Differences between getting the length of the array

Array

int length = array.length;

ArrayList

int length = array.size();

For Each Loop

This is a special loop in where you tell it to go through all the elements of the array, without specifying an index.

for (String b : buildings) {
    System.out.print(b);
}