Notes on Collections - 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-f8a-1
koofers-user-f8a-1 🇺🇸

5

(1)

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 12
Read: 4.7 (skip 4.7.2 and 4.7.3) and 4.8
Chapter 4 - Collections
A couple of lectures ago, I said that there were two kinds of collections
- arrays
- collections classes defined in the Java Library
We looked at arrays, which have some properties
- fixed size
- can store objects or primitives
- part of the language – special syntax for declaring and accessing arrays
Let's look at one of the Collections provided by the Java Library – the ArrayList class.
Class ArrayList
The Java language comes with hundreds of classes that we can use. These classes
implement commonly needed functions, and allow Java programmers to save time by
using these classes instead of having to write their own.
These classes are stored the Java Library, and are organized into packages. Each package
contains many classes.
Let’s look at the Java documentation.
We are going to look at one of these many classes: ArrayList
Note: The book was written using Java 1.4. We are using Java 1.5, and there are some
differences. One of these differences has to do with how collections work. This means
that the code in the book is NOT identical to the code we will be discussing.
Look at Notebook project (notebook1)
Note that when you compile the Notebook, you get an error message. Ignore it for now,
we’ll get back to this shortly.
The first statement is used to tell Java that we are using a class from the Library. If we
leave this line out, Java doesn’t know where to look for the ArrayList class.
The import statements come before the class declaration.
Once we have imported a class, we can use it just as if it was a class we had written
ourselves. We can create objects of that type and call methods on those objects.
pf3
pf4
pf5

Partial preview of the text

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

CSIS 110 - Lecture 12

Read: 4.7 (skip 4.7.2 and 4.7.3) and 4. Chapter 4 - Collections A couple of lectures ago, I said that there were two kinds of collections

  • arrays
  • collections classes defined in the Java Library We looked at arrays, which have some properties
  • fixed size
  • can store objects or primitives
  • part of the language – special syntax for declaring and accessing arrays Let's look at one of the Collections provided by the Java Library – the ArrayList class. Class ArrayList The Java language comes with hundreds of classes that we can use. These classes implement commonly needed functions, and allow Java programmers to save time by using these classes instead of having to write their own. These classes are stored the Java Library , and are organized into packages. Each package contains many classes. Let’s look at the Java documentation. We are going to look at one of these many classes: ArrayList Note: The book was written using Java 1.4. We are using Java 1.5, and there are some differences. One of these differences has to do with how collections work. This means that the code in the book is NOT identical to the code we will be discussing. Look at Notebook project (notebook1) Note that when you compile the Notebook, you get an error message. Ignore it for now, we’ll get back to this shortly. The first statement is used to tell Java that we are using a class from the Library. If we leave this line out, Java doesn’t know where to look for the ArrayList class. The import statements come before the class declaration. Once we have imported a class, we can use it just as if it was a class we had written ourselves. We can create objects of that type and call methods on those objects.

private ArrayList notes; This statement declares a variable named notes that is an ArrayList. Because ArrayList is a class, we need to create an object of this type. notes = new ArrayList(); Once we have an ArrayList object, we can use its methods. Although ArrayList has many methods, we are only going to look at 3 of them: add, get, and size. add – store an object in the list size – return the number of objects in the list (kind of like the .length field of an array) get – get an object from the list (but don’t remove it) Let’s look at how an ArrayList might work. See figures 4.1 and 4.2 (page 83) 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. We can retrieve the items in the same order as we added them. Look back at the Notebook source – it looks pretty simple, as it contains only 1 instance variable. All the difficult work is done in the ArrayList – this is one of the great advantages of using library classes: someone else has taken the time to develop something useful, and we can use it. Abstraction again! Discuss Notebook constructor, storeNote, and numberOfNotes methods. Point out that the Notebook doesn’t keep track of the number of notes – it lets the ArrayList do that instead. Numbering with collections Did you notice anything odd or surprising when you used the showNote method? It wanted a parameter that indicated the Note number to show. What values did you have to use? What value do you have to use to display the first Note that you inserted? A value that is used to reference a particular item in a collection is called an index. In an arrayList, the value of the index ranges from 0 to size-1 – Just like the index used with array elements. Look at the showNote method – most of the code is there to make sure that you don’t try to access the ArrayList outside of its valid range.

The while statement works this way:

  1. Evaluate the boolean expression. If it is true go to step 2. Otherwise, go to step 4.
  2. Execute the statements in the while-loop body.
  3. Go to step 1
  4. Execute the statement following the while loop. [Draw a flow chart] [Add a listNotes method to the notebook-j5 project – it needs this while loop int index = 0; while (index < notes.size() ) { System.out.println( notes.get( index ) ); index++; } Discuss practical parts of this loop:
  1. Preparation for first iteration
  2. While statement w/ boolean expression
  3. Statements that we want to execute repetitively
  4. Preparation for next iteration Use debugger to iterate through the list method in notebook-j5-Brian project

Lab Exercise Open the project student – it contains two classes:

  • Student: A class to keep track of a student’s name and GPA
  • StudentBody: A class to keep track of all registered students The Student class includes an instance variable GPA whose type is double. This type is used for floating point numbers. Add the following methods to StudentBody:
  1. printStudents: print a list of all students in the StudentBody (one student per line).
  2. printDeansList: print the heading “Dean’s List:” followed by a list of all students whose GPA >= 3.5 (one student per line).
  3. Modify your printDeansList method as follows: If the Dean's List is empty (that is, there were no students with a GPA >= 3.5), print the message “No students qualify”. Extra Credit:
  4. Add a method averageGPA to StudentBody that returns the average GPA of all students in the StudentBody. Email the file StudentBody.java to me – use the Subject "Lab 9"