220 likes | 252 Views
Chapter 2 : List ADT Part I – Sequential List. Overview. 2.1 Introduction to Lists 2.1.1 What is Lists? 2.1.2 Types of Lists 2.2 Sequential Lists 2.2.1 Implementation 2.2.2 Application 2.3 Linked Lists 2.3.1 Implementation 2.3.2 Application 2.4 Other types of Lists . Introduction.
E N D
Overview 2.1 Introduction to Lists 2.1.1 What is Lists? 2.1.2 Types of Lists 2.2 Sequential Lists 2.2.1 Implementation 2.2.2 Application 2.3 Linked Lists 2.3.1 Implementation 2.3.2 Application 2.4 Other types of Lists
Introduction • List is an ordered sequence of the same type elements (example: telephone directory, students) • List provides methods for : a) manipulating a specified range of elements, b) searching for elements c) access the elements d) delete the elements
[0] [1] [2] [3] [4] Introduction • List is divided by 2 types: a) Sequential List • Arrays structure – store elements in a contiguous memory b) Linked List • Linked structure – store elements anywhere in memory and linked by a reference/pointer
Sequential List • Sequential list contains a number of elements that is less than or equal to its capacity. • Java provides a resizable-array implementation for a sequential list known as ArrayList (dynamic application) • The advantages of using Sequential list: a) Appropriate data structure - when the number of data is predictable. b) Simple to sort data - sorting can be done easily after all the data has been stored (ascending or descending order).
Sequential List (cont …) • Implementation of : • ArrayList – like array structure whose objects can grow and shrink in response to a program’s storage requirements • In Java, ArrayList can be accessed from package ( java.util.* ) • Example: // create an empty list ArrayList ListName = new ArrayList( ); //create list with capacity 50 ArrayList ListName = new ArrayList (50); ArrayList Sequential List
ArrayList (cont …) • The main methods in ArrayList (provided by Java package) are : • Constructor construct a list • Add adds a new node • Set updates the contents of a node • Remove removes a node • Get returns the value at a specified index • IndexOf returns the index in the list of a specified element • Sizereturns the size (number of elements in the list) • isEmpty return true if the list is Empty (contains no elements)
ArrayList (cont …) a) Constructor ArrayList ( ) - construct an empty list Example : ArrayList stuList= new ArrayList ( ) ArrayList ( int initialCapacity) - construct a list with the specified initial capacity Example : ArrayList stuList = new ArrayList (10)
ArrayList (cont …) b) Add Void add (int , Object) - insert the specified element (object) at the specified location Example : stuList.add (3,”Fahrin”); c) Set Object set (int , Object ) Example : stuList.set (7, “Anuar”);
ArrayList (cont …) d) Remove Object remove (int) Example : stuList.remove (5); e) Get Object get (int) Example : stuList.get (7);
ArrayList (cont …) f) IndexOf Int indexOf (Object); Example : stuList.IndexOf(“zaki”); g) Size Example : int stu; stu= stuList.size ( ); or for (int x=0; x< stuList.size ( ); x++)
ArrayList (cont …) h) isEmpty Example : if stuList.isEmpty ( )
ArrayList (cont …) • Determine the number of elements ArrayList fruits = new ArrayList(); fruits.add (“apples”); fruits.add (“oranges”); fruits.add (“durian”); fruits.add (“apples”); System.out.println (fruits.size());
ArrayList (cont …) • Returns the elements at specified index ArrayList fruits = new ArrayList(); fruits.add (“apples”); fruits.add (“oranges”); fruits.add (“durian”); fruits.add (“apples”); System.out.println (fruits.get(2));
ArrayList (cont …) • Replaces the element at the specified index in this ArrayList ArrayList fruits = new ArrayList(); fruits.add (“apples”); fruits.add (“oranges”); fruits.add (“durian”); fruits.add (“apples”); System.out.println (fruits.set(2, “banana”));
ArrayList (cont …) • Example of sequential list using ArrayList class with primitive data import java.util.*; class Erase { public static void main(String[] args) { ArrayList names = new ArrayList(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); System.out.println(names.size()); names.remove (2); System.out.println(names); } }
Exercises • Write Java program segments to: • Create two sequential lists named liststudent1 and liststudent2 • Create two sequential lists named Book1 and Book2 • Insert 20 students into liststudent1 • Store 10 floating point numbers into ArrayList named NumberList • Insert 10 books into Book1 • Search for the student who has the studId = 188 from listStudent1