Topic 2-List (Sequential List) Arraylist Class: Edited by Miss Nafisah Amin
Topic 2-List (Sequential List) Arraylist Class: Edited by Miss Nafisah Amin
ArrayList class
Edited by
Miss Nafisah Amin
Chapter Objective
Learn about lists interface
Learn a few method of ArrayList class
Explore how various operations, such as search, insert,
and remove, on lists are implemented
Comparison with an Array
Java Collection Framework
is a set of classes and interfaces that implement
commonly reusable collection data structures.
Part of Java Collection Framework hierarchy
dealing with the List interface;
interface
Collection
interface
List
AbstractList
ArrayList LinkedList
Introduction - List
The Collection interface is the root interface for
manipulating collection objects.
Java Collections Framework’s (JCF) List interface extends
the Collection interface by providing some index related
methods as either a parameter or a return type.
List: A collection of elements of the same type
Length of list is number of elements in list
In any list objects, the elements are stored in sequence,
according to an index
Lists are implemented in the JCF via the java.util.List
interface.
It defines a list as essentially a more flexible version of an
array. Elements have a specific order, and duplicate
elements are allowed. Elements can be placed in a specific
position. They can also be searched for within the list.
Two concrete classes implement List.
ArrayList
LinkedList
List Interface
Refer to interface List in java.util.List package
(http://docs.oracle.com/javase/6/docs/api/java/util/List.html)
2 E set (int index, E //Replaces the element that was at //position index in this List object with //the parameter
element); element, and return //the previous occupant.
3 int indexOf (Object //Returns the index of the first //occurrence of element in this object, if //element appears in this
element); List object. //Otherwise, returns –1
4 void add (int index, E //Insert element at position index in this List //object; every element that was at a position //>=
element); index before this call is now at the next //higher position
5 E remove (int index); //Removes and return the element at //position index in this List object; every //element that
was at a position > //index before this call is now at the //next lower position
Adding Element :
public void add(E element); Insert object at the back of arraylist.
public void add(int index, E element); Insert object at position index and move the
elements up.
Replacing Element :
public E set(int index, E element); Set element at position index to object element
Searching:
public boolean contains(Object element); Returns true if ArrayList contains object.
public int indexOf(Object element); Returns index of first occurrence of object, or -1 if
not there.
public int lastIndexOf(Object element); Returns index of last occurrence of object, or -1 if
not there.
Removing Element :
public void clear(); removes all elements from ArrayList.
public boolean remove(int i); removes the element at position i.
removes the elements from positions I thru j
public boolean removeRange(int I, int j);
Other :
public int size(); Returns the number of elements in ArrayList.
Example
Exercise 1
1. Write Java program segments to create a
sequential lists named arrlist.
2. Write Java program segments to prompts user to
enter 10 integer numbers and store them into
arrlist .
2.1 Display the even number in the arrlist.
2.2 Remove the odd number from the arrlist.
2.3 Display all element in arrlist
Exercise 2
Given the following ArrayList ADT and Java application intList:
4 You can create array of any But, or the ArrayList object, each element
type such as int. must be either of type Object or subclass of
Object.
ArrayList with an Object class
Example
Customer Class
Test Class
Exercise 3
ArrayList object named staff that have 100 information
about staff. The information include; name, staff id, grade,
and address. Create the object (Staff) and insert into the
ArrayList;
Exercises
Reference
William J. Collins, Data Structures and the Java
Collections Framework, 2nd Edition, McGraw
Hill.
D.S Malik, P.S. Nair, Data Structure using Java,
2003, Course Technology.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Ar
rayList.html