Professional Documents
Culture Documents
Roslan Sadjirin
At the end of the lesson, student should be
able to:
Understand the concept of Sequential List
Define and use the ADT of Sequential List and
its operations
List, explain and apply the Sequential List in
solving a problem
A linear collection of elements
Support indexing of its elements.
ArrayList/Sequential List - Each element is
associated with an indexable position in the
list, such that every position except the first
has a unique predecessor and every position
except the last has a unique successor.
LinkedList: The first position in the list is
called as HEAD and the last position as TAIL.
The size of the list indicates the number of
element in the list.
NULL element is not allowed in the List.
A list is a popular data structure for storing
data in sequential order – for example
◦ A list of students
◦ A list of available rooms
◦ A list of cities
◦ A list of books.
Retrieve an element from a list.
Insert a new element to a list.
Delete an element from a list.
Find how many elements are in a list.
Find whether an element is in a list.
Find whether a list is empty.
Sequential List (Array List) - cover in Topic
2
Linked List – cover in Topic 3
Sequential List is a sequential data structures
Also called ArrayList
An array-based implementation, but it is an
improved version of one-dimensional array.
Declaration and creation of ArrayList:
◦ ArrayList list = new ArrayList()
◦ ArrayList<Object_Type> list = new
ArrayList<Object_Type>()
ArrayList object supports random access of
its elements; any element can be accessed in
constant time, given only the index of the
element.
ArrayList’s object size is automatically
maintained (expand/shrink) during the
execution of a program.
There are ArrayList method for inserting and
deleting at any index.
This method inserts the element at the back
of the ArrayList object.
Suppose fruits is (a reference to) the ArrayList
object, we can insert elements at the end of
the ArrayList object as follow:
◦ fruits.add(“rambutan”);
◦ fruits.add(“apple”);
◦ fruits.add(“durian”);
◦ fruits.add(“apple”);
The representation of sequence of list fruits
is as follow:
0 1 2 3
“rambutan” “apple” “durian” “apple”
Suppose that fruits is the ArrayList object as
shown below:
0 1 2 3
“rambutan” “apple” “durian” “apple”
Would be:
rambutan banana apple kiwi apple
Suppose that fruits is the ArrayList object as
shown below:
0 1 2 3 4
“rambutan” “banana” “apple” “kiwi” “apple”
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty,
and the size is " + progCourse.size());
else
System.out.println("The list progCourse is not
empty, and the size is " + progCourse.size());
progCourse.add("CSC128");
System.out.println("Elements of list progCourse : " +
progCourse);
System.out.println("CSC128 is at index " +
progCourse.indexOf("CSC128"));
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty,
and the size is " + progCourse.size());
else
System.out.println("The list progCourse is not
empty, and the size is " + progCourse.size());
progCourse.add("CSC138");
System.out.println("Elements of list progCourse : " +
progCourse);
System.out.println("CSC128 is at index " +
progCourse.indexOf("CSC128"));
System.out.println("CSC138 is at index " +
progCourse.indexOf("CSC138"));
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty,
and the size is " + progCourse.size());
else
System.out.println("The list progCourse is not
empty, and the size is " + progCourse.size());
progCourse.add(0, "CSC119");
System.out.println("Elements of list progCourse : " +
progCourse);
System.out.println("CSC119 is at index " +
progCourse.indexOf("CSC119"));
System.out.println("CSC128 is at index " +
progCourse.indexOf("CSC128"));
System.out.println("CSC138 is at index " +
progCourse.indexOf("CSC138"));
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty,
and the size is " + progCourse.size());
else
System.out.println("The list progCourse is not
empty, and the size is " + progCourse.size());
progCourse.add("CSC248");
System.out.println("Elements of list progCourse : " +
progCourse);
System.out.println("CSC119 is at index " +
progCourse.indexOf("CSC119"));
System.out.println("CSC128 is at index " +
progCourse.indexOf("CSC128"));
System.out.println("CSC138 is at index " +
progCourse.indexOf("CSC138"));
System.out.println("CSC248 is at index " +
progCourse.indexOf("CSC248"));
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty,
and the size is " + progCourse.size());
else
System.out.println("The list progCourse is not
empty, and the size is " + progCourse.size());
progCourse.add(3, "CSC238");
System.out.println("Elements of list progCourse : " +
progCourse);
System.out.println("CSC119 is at index " +
progCourse.indexOf("CSC119"));
System.out.println("CSC128 is at index " +
progCourse.indexOf("CSC128"));
System.out.println("CSC138 is at index " +
progCourse.indexOf("CSC138"));
System.out.println("CSC238 is at index " +
progCourse.indexOf("CSC238"));
System.out.println("CSC248 is at index " +
progCourse.indexOf("CSC248"));
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty,
and the size is " + progCourse.size());
else
System.out.println("The list progCourse is not
empty, and the size is " + progCourse.size());
progCourse.add("CSC308");
System.out.println("Elements of list progCourse : " +
progCourse);
System.out.println("CSC119 is at index " +
progCourse.indexOf("CSC119"));
System.out.println("CSC128 is at index " +
progCourse.indexOf("CSC128"));
System.out.println("CSC138 is at index " +
progCourse.indexOf("CSC138"));
System.out.println("CSC238 is at index " +
progCourse.indexOf("CSC238"));
System.out.println("CSC248 is at index " +
progCourse.indexOf("CSC248"));
System.out.println("CSC308 is at index " +
progCourse.indexOf("CSC308"));
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty, and the
size is " + progCourse.size());
else
System.out.println("The list progCourse is not empty, and
the size is " + progCourse.size());
progCourse.set(0, "CSC118");
progCourse.set(5, "CSC305");
System.out.println("Elements of list progCourse : " +
progCourse);
System.out.println("CSC118 is at index " +
progCourse.indexOf("CSC118"));
System.out.println("CSC128 is at index " +
progCourse.indexOf("CSC128"));
System.out.println("CSC138 is at index " +
progCourse.indexOf("CSC138"));
System.out.println("CSC238 is at index " +
progCourse.indexOf("CSC238"));
System.out.println("CSC248 is at index " +
progCourse.indexOf("CSC248"));
System.out.println("CSC305 is at index " +
progCourse.indexOf("CSC305"));
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty, and the
size is " + progCourse.size());
else
System.out.println("The list progCourse is not empty,
and the size is " + progCourse.size());
if(progCourse.contains("CSC119"))
System.out.println("CSC119 is in the list");
else
System.out.println("CSC119 is not in the list");
if(progCourse.contains("CSC305"))
System.out.println("CSC305 is in the list");
else
System.out.println("CSC305 is not in the list");
if (progCourse.remove("CSC118"))
System.out.println("CSC118 has been removed");
System.out.println();
progCourse.add(0, "CSC118");
progCourse.remove(2);
System.out.println();
progCourse.add(2,"CSC138");
if(progCourse.isEmpty())
System.out.println("The list progCourse is empty,
and the size is " + progCourse.size());
else
System.out.println("The list progCourse is not
empty, and the size is " + progCourse.size());
Interface
Collection
List
Abstract Class
AbstractList
Concrete Class
ArrayList
public interface MyList<E> {
}
public abstract class MyAbstractList<E> implements MyList<E> {
public MyArrayList(){}
data[index] = e;
size++;
}
public void add(E e) {
ensureCapacity();
data[size++] = e;
}
private void ensureCapacity() {
if(size >= data.length) {
E[] newData = (E[])
(new Object[size * 2 + 1]);
System.arraycopy(data, 0, newData, 0, size);
data=newData;
}
}
if(size != data.length) {
E[] newData = (E[]) (new
Object[size]);
System.arraycopy(data, 0,
newData, 0, size);
data = newData;
}
}
} //end of MyArrayList
public class Student {
private String id;
private String name;
private String program;
private int part;
private double cgpa;
//constructors
//mutators
//accessors
//printer
}
This program will:
◦ accept five (5) students and stored in the ArrayList
object (theList)
◦ display the theList
◦ find and display the best student
◦ count and display the number of student in part 4
◦ count and display the number of student who CGPA
scored is 3.00 and above
public class StudentApp {
public static void main(String[] args) {
}
/*find the best student
*count the number of student in part 4
*count the number of students who CGPA scored is 3.00 */
} //end of main
} //end of class
End of Topic 2
Question?