0% found this document useful (0 votes)
48 views

2 SequentialList

The document discusses sequential lists and array lists. It begins by defining sequential lists and their properties like supporting indexing and having predecessors and successors. It then discusses array list operations like retrieving, inserting, deleting elements and checking size. Examples are provided to demonstrate inserting, accessing, updating and removing elements from an array list. The document ends with code examples using an ArrayList to apply these operations and demonstrate the behavior of the list.

Uploaded by

ain najjaah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

2 SequentialList

The document discusses sequential lists and array lists. It begins by defining sequential lists and their properties like supporting indexing and having predecessors and successors. It then discusses array list operations like retrieving, inserting, deleting elements and checking size. Examples are provided to demonstrate inserting, accessing, updating and removing elements from an array list. The document ends with code examples using an ArrayList to apply these operations and demonstrate the behavior of the list.

Uploaded by

ain najjaah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

TOPIC 2

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”

 Then fruits.size() returns the value 4.


 Suppose that fruits is the ArrayList object as
shown below:
0 1 2 3
“rambutan” “apple” “durian” “apple”

 Then fruits.get(1) would return “apple”


 Suppose that fruits is the ArrayList object as
show below:
0 1 2 3
“rambutan” “apple” “durian” “apple”

 We can change the element at index 2 as follow:


◦ fruits.set(2, “kiwi”);

 The elements in the ArrayList object fruits are


now “rambutan”, “apple”, “kiwi”, and “apple”
 suppose that fruits is the ArrayList object as
shown below:
0 1 2 3
“rambutan” “apple” “durian” “apple”
 Then the output from:
fruits.add(1, “banana”);
for (int i = 0; i<fruits.size(); i++)
System.out.print(fruits.get(i) + “ ”);

 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 the statement fruits.remove(2); is executed,


then fruits will consist of:
rambutan banana kiwi apple
 Suppose that fruits is the ArrayList object as
shown below:
0 1 2 3 4
“rambutan” “banana” “apple” “kiwi” “apple”

 If the statement is fruits.indexOf(“kiwi”); is


executed, then the value returned will be 3.
 But if the statement is
fruits.indexOf(“durian”); then the value
returned will be -1.
 Suppose fruits is the ArrayList object:
0 1 2 3 4
“rambutan” “banana” “apple” “kiwi” “apple”

 If the call is fruits.clear(); then after the


execution of the method, fruits will be an
empty ArrayList object.
ArrayList<String> progCourse = new ArrayList<String>();
System.out.println("Elements of list progCourse : " +
progCourse);

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("Elements of list progCourse : " +


progCourse);

System.out.println();

progCourse.add(0, "CSC118");

System.out.println("After adding back CSC118 into the


list, the elements of list progCourse now is: " +
progCourse);
System.out.println("Statement progCourse.remove(2) caused
the list in index 2 to be removed. ");

progCourse.remove(2);

System.out.println("The elements of list progCourse is now


: " + progCourse);

System.out.println();
progCourse.add(2,"CSC138");

System.out.println("After adding back CSC138 into the


list, the elements of list progCourse now is : " +
progCourse);
ArrayList finalList = (ArrayList)progCourse.clone();
progCourse.clear();

System.out.println("After statement of ArrayList finalList


= (ArrayList)progCourse.clone();");

System.out.println("Elements of list finalList is : " +


finalList);

System.out.println("Elements of list progCourse is : " +


progCourse);

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 void add(E e);


public void add(int index, E e);
public void clear();
public boolean contains(E e);
public E get(int index);
public int indexOf(E e);
public boolean remove(E e);
public E remove(int index);
public E set(int index, E e);
public int size();

}
public abstract class MyAbstractList<E> implements MyList<E> {

protected int size = 0;


protected MyAbstractList(){}
protected MyAbstractList(E[] objects){
for (int i=0; i<objects.length; i++)
add(objects[i]);
}
public void add(E e) { add(size, e); }
public boolean isEmpty(){ return size == 0; }
public int size(){ return size; }
public boolean remove(E e) {
if(indexOf(e) >= 0){
remove(indexOf(e));
return true;
}
else
return false;
}
}
public class MyArrayList<E> extends MyAbstractList<E> {

public static final int INITIAL_CAPACITY = 16;


private E[] data = (E[]) new Object[INITIAL_CAPACITY];

public MyArrayList(){}

public MyArrayList(E[] objects)


{
for(int i=0; i<objects.length; i++)
add(objects[i]);
}
public void add(int index, E e) {
ensureCapacity();

for (int i=size-1; i>=index; i--)


data[i+1] = data[i];

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;
}
}

public void clear() {


data = (E[]) new Object[INITIAL_CAPACITY];
size=0;
}
public boolean contains(E e) {
for (int i=0; i<size; i++)
if(e.equals(data[i]))
return true;
return false;
}

public E get(int index) {


return data[index];
}
public int indexOf(E e) {
for(int i=0; i<size; i++)
if(e.equals(data[i]))
return i;
return -1;
}

public int lastIndexOf(E e)


{
for(int i=size-1; i>= 0; i--)
if(e.equals(data[i]))
return i;
return -1;
}
public E remove(int index) {
E e = data[index];
for(int j=index; j<size-1; j++)
data[j]=data[j+1];
data[size-1] = null;
size--;
return e;
}
public E set(int index, E e) {
E old = data[index];
data[index] = e;
return old;
}
public String toString() {
StringBuilder result = new StringBuilder("[");

for(int i =0; i<size(); i++) {


result.append(data[i]);
if(i<size-1)
result.append(", ");
}
return result.toString() + "]";
}
public void trimToSize() {

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) {

MyArrayList<Student> theList = new MyArrayList<Student>();

/*Input 5 student into theList*/


for (int i=0; i<5; i++) {
Scanner in = new Scanner(System.in);

System.out.print("Enter student ID : ");


int studId = in.nextInt();

System.out.print("Student name : ");


String studName = in.next();

System.out.print("Enter part : ");


int studPart = in.nextInt();

System.out.print("Enter CGPA : ");


double studCgpa = in.nextDouble();

Student student = new Student(studId, studName,


studPart, studCgpa);
theList.add(student);
}
/*Display all students in theList*/

Student student = null;


for (int i=0; i<theList.size();i++) {
student = (Student)theList.get(i);
System.out.println(student.toString());

}
/*find the best student
*count the number of student in part 4
*count the number of students who CGPA scored is 3.00 */

int part4=0, scorer=0, index=0;


double cgpa = 0.0;
for (int i=0; i<theList.size(); i++) {
student = (Student) theList.get(i);
if (student.getCgpa() > cgpa) {
cgpa = student.getCgpa();
index = i;
}
if (student.getPart() == 4)
part4++;
if(student.getCgpa() >= 3.00)
scorer++;
}
System.out.println("The best student is " );
student = (Student) theList.get(index);
System.out.println(student.toString());
System.out.println("No. of part 4 student : " + part4);
System.out.println("No. of student with CGPA 3.00 and
above : " + scorer);

} //end of main

} //end of class
End of Topic 2

Question?

You might also like