Java Collections: Using ArrayLists to Store and Manage Quiz Scores - Prof. Brian F. Hanks, Study notes of Javascript programming

How to use java's arraylist class to store and manage quiz scores. The basics of arraylists, including their properties, methods, and how they differ from arrays. The document also includes an explanation of indexing and parameterized types or generics in the context of arraylists. A lecture note for csis 110 students using java 1.5.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-3jl-1
koofers-user-3jl-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 9
Quiz Results (Before EC)
mean 19.6
stdev 3.9
median 18.5
max 25
Distribution (after EC):
<15: 1 15-17: 4 17.5 – 19.5: 3 20 – 22: 3 22.5 – 24.5: 1 25+: 6
Last time: this, debugger
Chapter 4 - Collections
A couple of comments:
1) I’m doing this chapter out of order
2) Some of the code we look at will be a little different than the examples in the
book, because we are using Java 1.5
Collections
Let's consider the quiz scores that we just discussed. Suppose I wanted to write a Java
program that kept track of the quiz scores. What variables would I need?
Now, suppose I wanted to determine the minimum and maximum scores from these
variables.
What if I was teaching at a school where there were 100 students in a class?
So, this is very awkward, and not very flexible.
I'd like to be able to have a general mechanism for keeping a set of values in a list. Java
provides two mechanisms for this:
1) Collection classes in the Java library.
2) Arrays.
We’ll look at one type of Collection first.
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.
pf3

Partial preview of the text

Download Java Collections: Using ArrayLists to Store and Manage Quiz Scores - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 9

Quiz Results (Before EC) mean 19. stdev 3. median 18. max 25 Distribution (after EC): <15: 1 15-17: 4 17.5 – 19.5: 3 20 – 22: 3 22.5 – 24.5: 1 25+: 6 Last time: this, debugger Chapter 4 - Collections A couple of comments:

  1. I’m doing this chapter out of order
  2. Some of the code we look at will be a little different than the examples in the book, because we are using Java 1. Collections Let's consider the quiz scores that we just discussed. Suppose I wanted to write a Java program that kept track of the quiz scores. What variables would I need? Now, suppose I wanted to determine the minimum and maximum scores from these variables. What if I was teaching at a school where there were 100 students in a class? So, this is very awkward, and not very flexible. I'd like to be able to have a general mechanism for keeping a set of values in a list. Java provides two mechanisms for this:
  3. Collection classes in the Java library.
  4. Arrays. We’ll look at one type of Collection first. 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 difference 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 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.