background image

1 - 2 

Fundamentals of Java Programming Lab 14.3.5 

Copyright 

 2003, Cisco Systems, Inc. 

Lab 14.3.5  An ArrayList 

 
Estimated Time:
 30 minutes  
 
Learning Objectives:  

•  In this lab activity, the student will use the Collections class ArrayList to store objects.

 

 
Description/Scenario: 

•  Collection objects represent references to other objects. 

•  There are four basic storage technologies available for storing objects.  They are array, 

linked list, tree, and hash table. 

•  Objects that can serve as containers for other objects can be categorized as collections, 

lists, sets, or maps. 

•  Lists are ordered collections, and they can have duplicates. The order can be the natural 

order, which is the order in which the objects were added to the list. Because the list is 
ordered, objects in a list can be indexed. An array is an example of a list. The collection 
framework includes classes that provide for dynamic lists. This type of storage is also 
known as a bag or multiset. Other names for this type of storage include list and 
sequence. 

•  The programmer can create classes that implement the collections interface to manage 

and store objects. However, an extensive group of classes are available that have 
implemented one or more of the collection interfaces and storage methods.  The 
ArrayList class extends from AbstractList, which extends from AbstractCollection.  
ArrayList implements the List interface.  

 

 
File Management: 

Open BlueJ. Click on Project from the BlueJ main menu and select New. In the New 
Project window and in the Look in: list box select c:\. Now, double click the javacourse 
folder listed in the text window and a different New Project window opens with javacourse 
in the Look in: list box. Now, double click the chap14 folder listed in the text window and a 
different New Project window opens with chap14 in the Look in: list box. Type lab14.3.5 
in the File name text box and click on Create to create a lab14.3.5 subfolder in the 
chap14 folder.  

 
Tasks:
 
 

Step 1  Use of ArrayList 

a.  Define an ArrayList called points to store Point objects in a class called Line. Lists 

are ordered collections and can have duplicates. The order can be the natural order, 
that is, the order in which the objects were added to the list. Because the list is 
ordered, objects in a list can be indexed. An array is an example of a list.  

b. Define 

an 

addPoint(Point p) method which takes a Point object as an argument. 

The class Point stores the x and y coordinates of a two-dimensional point and 
implements the toString() method to return a textual representation of a point.  

c. Include 

import java.awt.Point statement to implement the Point class. The 

addPoint() method uses the add() method of the ArrayList points to add the 

points. 

d.  Define a method numberOfPoints() which returns the number of points (Hint: Use 

the size() method of the ArrayList to determine the size of the List). 

 

background image

2 - 2 

Fundamentals of Java Programming Lab 14.3.5 

Copyright 

 2003, Cisco Systems, Inc. 

Step 2  Testing the program 
 

a. In 

the 

main method create an instance of Line called line. Use the addPoint() 

method to add points to the ArrayList. Sample code:  
 
line.addPoint(new Point(17, 102));  

 
Similarly add Points like (678, 56). Using the System.out.println() method, print 

the number of points. 

 

 

 


Document Outline