Files
G4G0-1/Semester 1/Programming 1/Week 9/Week 9 Programming 1.md
2024-01-16 13:48:46 +00:00

1.1 KiB

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

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

public void showAllAnimals()
{
	int listPosition = 0;
	while( listPosition < 6 )
	{
		showAnimal( listPosition );
		listPosition++;
	}
}

Sentinel

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
	}
}