Laboratory Comments - Introduction to Programming in Java | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-hn4-1
koofers-user-hn4-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 12
Lab Comments
Discuss isEmpty() method: most people had something like:
if ( notes.size() == 0 ) // or ( numberOfNotes() == 0 )
{
return true;
}
else
{
return false;
}
Discuss simplifying this.
ArrayList
Last time we looked at the ArrayList class, which is provided by the Java library. It is a
type of Collection, which means that it can be used to store objects of some other type.
What are some of the methods provided by the ArrayList?
add – store an object at the end of the list
size – return the number of objects in the list
get – get an object from the list (but don’t remove it)
Properties of ArrayLists:
Variable size – an ArrayList gets bigger if necessary as we add more items
It keeps track of the number of items it currently contains
It maintains the order of the items.
The ArrayList is an example of a generic or parameterized class. This means that when
we create an ArrayList object, we have to specify the type of objects that will be stored in
the ArrayList.
Let's look at some other declarations:
private ArrayList<String> notes;
private ArrayList<MailItem> inbox;
private ArrayList<Book> library;
What is the type of the variable? What is the type of the elements being stored in the
ArrayList?
pf3

Partial preview of the text

Download Laboratory Comments - Introduction to Programming in Java | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 12

Lab Comments Discuss isEmpty() method: most people had something like: if ( notes.size() == 0 ) // or ( numberOfNotes() == 0 ) { return true; } else { return false; } Discuss simplifying this. ArrayList Last time we looked at the ArrayList class, which is provided by the Java library. It is a type of Collection, which means that it can be used to store objects of some other type. What are some of the methods provided by the ArrayList? add – store an object at the end of the list size – return the number of objects in the list get – get an object from the list (but don’t remove it) Properties of ArrayLists:  Variable size – an ArrayList gets bigger if necessary as we add more items  It keeps track of the number of items it currently contains  It maintains the order of the items. The ArrayList is an example of a generic or parameterized class. This means that when we create an ArrayList object, we have to specify the type of objects that will be stored in the ArrayList. Let's look at some other declarations: private ArrayList notes; private ArrayList inbox; private ArrayList library; What is the type of the variable? What is the type of the elements being stored in the ArrayList?

When we call the add method, what do we need to pass to it? When we call the get method, what is the type of object that will be returned? Accessing all of the elements in an ArrayList – The 'foreach' statement Let's go back to the Notebook example. We can create a Notebook, add Notes to it, and display the individual Notes. What would I need to do to write a method that prints all of the Notes in the Notebook? We could just call showNote(0), showNote(1), showNote(2), … What’s wrong with this approach? The Java language includes several ways to iterate through a collection of items. These allow us to repeat a set of statements for each item in a collection. The first of these is the foreach loop. for ( ElementType elementName : collection ) { loop body  use elementName in here } Basically, this says something like: For each element in the collection, execute the statements in the loop body. Question: how many times are the statements in the loop body executed? elementName is a LV – scope is the foreach loop. Examples:

  • ArrayList groceryList, want to display all the elements
  • ArrayList library, want to display the author and title of every book Let’s try to write a showAllNotes method in our Notebook class Use debugger to show how the loop works. Briefly discuss Lab project. Point out new type double.