ArrayList Program

Please read over these notes on ArrayLists

NOTE: There is a sample program at the bottom of this page

You're going to write a program that uses arrayLists.

Your program should do all of the following:

You need to know how to use both a regular for loop as well as a for-each loop for array lists.

★ You need to know how to delete an item from inside a for loop

 

First you need to think of something that you could have a collection of or a list of.
Maybe songs, movies, actors, books, ...

Secondly: Whatever thing you choose must have some properties.
(You need to have at least 3 pieces of information for your object).

For example, maybe I want to make a list of cheeses that I like.
My class woud be called Cheese and it would containg the following information about each cheese:

class Cheese {
   String name;
   int like; //0=bad, 5=amazing
   String usage;

   //I'll make a constructor too as that simplifies things a lot later on
   Cheese(String name, int like, String usage ) {
       this.name = name;
       this.like = like;
       this.usage = usage;
   }
}

I am using "cheeses" as my thing that I'm making a list of. I have 3 properties, a string, an int and a string.
PLEASE make up your own stuff. Don't just have "wheels" with a a string, int, and string. Do your own thing.

Third: make the array list. It should be a plural noun, or a noun that you can tell is a collection or a list, for example "cars" or, even better, "carList".

ArrayList<Cheese> cheeseList = new ArrayList<Cheese>();

This is how you make a Cheese entry and add it to the list:

Cheese ch = new Cheese("Parmesian",4,"on spaghetti");
cheeseList.add(ch);
//Note that you can use the same variable over again, you just can't create it again (not "Cheese ch")
ch = new Cheese("Old Cheddar",5,"to eat");
cheeseList.add(ch);
//But the fastest way is to do this since we don't actually care about the ch variable and will never use it again:
cheeseList.add( new Cheese("Mozzarella", 3, "on pizza") );  

Your program

DO NOT USE CHEESE AS YOUR EXAMPLE. It's my example. Find something else that you're intersted in.

Final Instructions

1. This is a TEXT program, so most of it will be in static void main(). You won't be using HSA2 and you won't be using gc (graphics console).

2. You can put the object that you're listing inside your main class.

3. Use methods for as many things as make sense (each separate task).

Example

import java.util.ArrayList;

public class CheeseListProgram {

	//GLOBAL VARIABLES
	static ArrayList cheeseList = new ArrayList();
	
	//CHEESE IS AN INNER CLASS (so I don't have to use two files. It's okay as long as the inner class is not too big).
	static class Cheese {
	   String name;
	   int like; //0=bad, 5=amazing
	   String usage;

	   //I'll make a constructor too as that simplifies things a lot later on
	   Cheese(String name, int like, String usage ) {
	       this.name = name;
	       this.like = like;
	       this.usage = usage;
	   }
	}

	public static void main(String[] args) {
		addCheeses();
		printList();

		//now print out all cheeses that go on pizza
		
		//now find all cheeses with like < 2 and delete them
		
		//now print out the list again
		
		//make sure that you've used a for-each loop somewhere

	}
	
	static void addCheeses() {
		cheeseList.add( new Cheese("Cheddar", 4, "on pizza") );
		cheeseList.add( new Cheese("Mozzarella", 3, "on pizza") );
		cheeseList.add( new Cheese("Brie", 5, "by itself") );
		cheeseList.add( new Cheese("Haloumi", 1, "not good") );
	}

	static void printList() {
		System.out.printf("%-15s %5s \t%-20s \n", "Name", "Like: 5=best", "Used for");
		System.out.println("============================================");
		for (int i = 0; i < cheeseList.size(); i++) {
			Cheese temp = cheeseList.get(i);
			System.out.printf("%-15s %5d \t\t%-20s \n", temp.name, temp.like, temp.usage);
		}
	}
}

Remember, your goal is to learn and understand this, not to just copy and modify it