mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-10-10 15:01:15 +00:00
Website snapshot
This commit is contained in:
parent
ee0ab66d73
commit
50ec3688a5
281 changed files with 21066 additions and 0 deletions
67
content/ta/spring2018/cpsc220/mar29.md
Normal file
67
content/ta/spring2018/cpsc220/mar29.md
Normal file
|
@ -0,0 +1,67 @@
|
|||
# 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
|
||||
|
||||
```java
|
||||
public enum Coin { PENNY, NICKEL, DIME, QUARTER}
|
||||
```
|
||||
|
||||
Now declare and initialize a variable
|
||||
|
||||
```java
|
||||
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
|
||||
|
||||
```java
|
||||
ArrayList<objectType> list = new ArrayList<objectType>();
|
||||
```
|
||||
|
||||
### Differences between getting the length of the array
|
||||
|
||||
**Array**
|
||||
|
||||
```java
|
||||
int length = array.length;
|
||||
```
|
||||
|
||||
**ArrayList**
|
||||
|
||||
```java
|
||||
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.
|
||||
|
||||
```java
|
||||
for (String b : buildings) {
|
||||
System.out.print(b);
|
||||
}
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue