first commit
This commit is contained in:
73
Semester 1/Programming 1/Week 9/Week 9 Programming 1.md
Normal file
73
Semester 1/Programming 1/Week 9/Week 9 Programming 1.md
Normal file
@@ -0,0 +1,73 @@
|
||||
## Lecture 1 (11:00)
|
||||
|
||||
### Flexible-Size Collections
|
||||
|
||||
Do not need to specify a size when we create a collection.
|
||||
Add as many items without concern for space.
|
||||
|
||||
### ArrayList
|
||||
- Imported from java.util
|
||||
-
|
||||
### ArrayList Methods
|
||||
- get()
|
||||
- remove()
|
||||
|
||||
## Lecture 2 (14:00)
|
||||
|
||||
### For Each Loop
|
||||
|
||||
```java
|
||||
for( Animal animal : animalCollection )
|
||||
{
|
||||
System.out.println(animal)
|
||||
}
|
||||
```
|
||||
|
||||
- The scope of `animal` is exclusively local to this loop body
|
||||
- `animal` is of the type Animal
|
||||
- For each element of type Animal in the `animalCollection` array list
|
||||
- Not good for searches.
|
||||
|
||||
### While Loop
|
||||
|
||||
```java
|
||||
public void showAllAnimals()
|
||||
{
|
||||
int listPosition = 0;
|
||||
while( listPosition < 6 )
|
||||
{
|
||||
showAnimal( listPosition );
|
||||
listPosition++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Sentinel
|
||||
|
||||
```java
|
||||
public boolean findAnimal( String myPet ) {
|
||||
|
||||
int listPosition = 0;
|
||||
boolean found = false;
|
||||
|
||||
while( listPosition < animalCollection.size() && found == false ) {
|
||||
|
||||
Animal animal = animalCollection.get( listPosition );
|
||||
String name = animal.getName();
|
||||
|
||||
if( name.equals( myPet ) ) {
|
||||
found = true;
|
||||
}
|
||||
else {
|
||||
listPosition++;
|
||||
}
|
||||
}
|
||||
|
||||
if( found == true ) {
|
||||
return true
|
||||
}
|
||||
else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user