Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to OOP with Java, C. Thomas Wu List

Similar presentations


Presentation on theme: "Intro to OOP with Java, C. Thomas Wu List"— Presentation transcript:

1 Intro to OOP with Java, C. Thomas Wu List
STIA1123 : Programming 2 List ©The McGraw-Hill Companies, Inc.

2 Objectives Understand the concept of List and its operations
Learn the various implementations of list: Array Implementation of List in Java Collection Framework Using ArrayList & Vector classes Linked List Implementation of List in Java Collection Framework Using LinkedList Know how to process list such as list traversal

3 What is a List? A list provides a way to organize data
Fig A to-do list.

4 What is a List? A list is a popular data structure to store data in sequential order. For example, a list of students, a list of available rooms, a list of cities, and a list of books, etc. can be stored using lists.

5 Conceptual View of a List
Linear List L = (e0, e1, e2, e3, …, en-1) relationships e0 is the zeroth (or front) element en-1 is the last element ei immediately precedes ei+1

6 Example Applications Example Lists
Students in F22AO2 = (Humphrey, Emma, Stuart…) Questions in exam in F22AO2 = (q1, q2, q3, q4) Days of the week = (Sun, Mon, Tue, Wed, Thu, Fri, Sat) World's longest rivers 10 best albums Players in a Team Notice that Days of Week is used as an example of both a data object and of a linear list. As a data object, {S, M, T, …} and {M, W, S, …} describe the same data object with 7 allowable instances S, M, T, … As a linear list (S, M, T, …) and (M, W, S, …) are two different instances of a linear list. Linear List itself may be viewed as data object, which is simply the set of all possible linear list instances.

7 List Operations Useful set of operations:
Add an element at a specified index Remove and return element with given index Determine list size Get element with given index Determine the index of an element

8 Add Element Assume L = (a,b,c,d,e,f,g)
Add an element at a specified index add(0,h) modified list L = (h,a,b,c,d,e,f,g) index of a,b,c,d,e,f, and g increase by 1 add(2,h) modified list L = (a,b,h,c,d,e,f,g) index of c,d,e,f, and g increase by 1 add(10,h) = error add(-6,h) = error

9 Add Element Assume L = (a,b,c,d,e,f,g) Add new entry at the end add(h)
modified list L = (a,b,c,d,e,f,g,h) - No need to specify the index

10 Remove Element Assume L = (a,b,c,d,e,f,g)
Remove and return element with given index remove(2) returns c and L becomes (a,b,d,e,f,g) index of d,e,f, and g decrease by 1 remove(-1) = error remove(20) = error

11 List Size and Get Element
Assume L = (a,b,c,d,e) Determine list size size = 5 Get element with given index get(0) = a get(2) = c get(4) = e get(-1) = error get(9) = error

12 Find Index of List Element
Assume L = (a,b,d,b,a) Determine the index of an element indexOf(d) = 2 indexOf(a) = 0 indexOf(z) = -1 The index of a nonexistent element is defined to be –1. When theElement is in the list an index between 0 and size()-1 is the result. So –1 would be an invalid index and is used to represent the case when theElement is not in the list

13 Example 1 2 The effect of list add(x),add(index,x), remove(index) operations on an initially empty list

14 List in Java List is one of the interfaces specified in the Java Collection Framework The JCF provides several types of collections, such as sets, lists, queues and maps in the form of interfaces. A collection is a container object that represents a group of objects, often referred to as elements

15 Java Collection Framework hierarchy (partial).
Set and List are subinterfaces of Collection.

16 The Collection Interface
The Collection interface is the root interface for manipulating a collection of objects.

17 The List Interface

18 The List interface Apart from methods specified in the Collection interface, it contains some methods such as: boolean add(E element) //inherited boolean add(int index, E element) E remove(int index) int size() //inherited E get(int index) int indexOf(Object o) int lastIndexOf(Object o)

19 List Implementations Several concrete classes implement the List interface such as: ArrayList Vector LinkedList They are categorised based on the type of data structures used to implement the lists: - Array (ArrayList & Vector) - Sequence of linked data or linked nodes (LinkedList)

20 Array–based List Implemented using Array
Size of array is fixed, therefore dynamic expansion must be possible if the capacity of array is exceeded (costly operation) Inserts & Deletes require moving elements (costly operation except item at the end of list) Eg method add(index,x), addAll(), remove(x) are inefficient Each element can be directly accessed (fast access) Eg. method get(), set() are efficient

21 Dynamic Array Expansion
An array has a fixed size When array becomes full, move its contents to a larger array create a new array eg. twice the current size copy data from original to new location the new array now becomes the current array.

22 Dynamic Array Expansion
The dynamic expansion of an array copies the array's contents to a larger second array.

23 List add() Methods add(x) method adds new item x at the end of the list Assign x at end Increment size of list by 1 add(index,x) method adds new item x in mid-list (at position index) Requires shifting ahead from last element until element at index Assign x at index

24 Adding Items in Mid-list
eg. add(2, “Carla”) Insertion point Making room to insert Carla as third entry in an array.

25 The remove() Method Must shift existing entries to avoid gap in the array Except when removing last entry In all cases, decrease size by 1 Method must also handle error situation When position specified in the remove is invalid When remove() is called and the list is empty Invalid call returns null value

26 Removing a List Entry eg. remove(“Bob”) or remove(1).
Removing Bob by shifting array entries.

27 Using ArrayList Class To create an ArrayList object:
ArrayList <Element Type> listvar = new ArrayList <Element Type>( ); eg. ArrayList numList = new ArrayList( ); ArrayList <String> names = new ArrayList <String>(); ArrayList <Student> students = new ArrayList <Student>();

28 Using ArrayList Class Once the ArrayList object is created, you can invokes all the operations by calling the related methods, e.g.: numList.add(10); names.add("Ali"); names.add(0, "Abu"); students.add(new Student("Siti", 10001)); students.add(new Student("John", 10002)); System.out.println(students.size()); System.out.println(names.get(1)); System.out.println(names.indexOf("Abu")); System.out.println(names.remove(1)); System.out.println(names.indexOf("Ali"));

29 The Vector Class In Java 2, Vector is the same as ArrayList, except that Vector contains the synchronized methods for accessing and modifying the vector. None of the new collection data structures introduced so far are synchronized. If you don’t need synchronization, use ArrayList since it work faster than Vector

30 The Vector Class, cont.

31 Using the Vector Class Vector vector = new Vector(); System.out.println("Before adding elements:“); System.out.println("Capacity = "+vector.capacity()); System.out.println("Size = "+vector.size()); vector.addElement(1); vector.addElement(2); vector.addElement(3); vector.insertElementAt(4,1); System.out.println("After adding elements:"); System.out.println("Capacity = "+vector.capacity()); System.out.println("Size = "+vector.size()); OUTPUT: Before adding elements: Capacity = 10 Size = 0 After adding elements: Size = 4

32 Linked-based List Implemented using Linked List
A linked list consists of a chain/sequence of nodes Each node is an object that contains two attributes: Element – store the element Next – store reference to its next neighbor (Null if this is the last node in the sequence)

33 Pros and Cons of using Linked List
The chain (list) can grow as large as necessary Can add and remove nodes without shifting existing entries But … Must traverse a chain to determine where to make addition/deletion Retrieving an entry requires traversal (As opposed to direct access in an array)

34 add(x) - Adding to the end List
A chain of nodes (a) just prior to adding a node at the end; (b) just after adding a node at the end.

35 add(index,x) – Adding item in mid-List
A chain of nodes (a) prior to adding node between adjacent nodes; (b) after adding node between adjacent nodes

36 The Method remove A chain of nodes (a) prior to removing first node; (b) after removing the first node

37 The Method remove A chain of nodes (a) prior to removing interior node; (b) after removing interior node

38 Using LinkedList Class
To create an LinkedList object: LinkedList <Element Type> listvar = new LinkedList <Element Type>( ); eg. LinkedList <Integer> numList = new LinkedList <Integer>(); LinkedList <String> names = new LinkedList <String>(); LinkedList <Student> students = new LinkedList <Student>();

39 List Processing: List traversal
Iterator enables us to iterate over or traverse a collection Iterator is an interface (in java.util) It contains only three methods: hasNext() next() remove() To get the iterator object, call the iterator() method that will return an implementation of these methods

40 Iterator for List traversal
Iterator Approach: Iterator it = list.iterator(); while (it.hasNext()) System.out.println(it.next());

41 JDK 1.5 Feature List Traversal Using a JDK 1.5, you can use enhanced “for each” loop without using an iterator, as follows: for (Object element : list) System.out.print(element + " ");

42 List Traversal with ArrayList
This example creates an array list with specified capacity, and inserts elements into the specified location & remove an element in the list. Finally, the example traverses the list using the iterator.

43 Using ArrayList Class }
ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(1); aList.add(2); aList.add(3); aList.add(0,10); aList.add(3,30); aList.remove(1); Iterator listItr = aList.iterator(); while (listItr.hasNext()) { System.out.println(listItr.next() + “ “); } OUTPUT:

44 List Traversal with LinkedList
This example creates a LinkedList, and inserts & removes elements from the list. Finally, the example traverses the list using the iterator.

45 List Traversal with LinkedList
LinkedList<String> lList = new LinkedList<String>(); lList.add("red"); lList.add(0,"green"); lList.add("blue"); lList.add(1,"yellow"); lList.remove(lList.size()-1); Iterator listItr = lList.iterator(); while (listItr.hasNext()) { System.out.println(listItr.next() + “ “); }

46 List Traversal with LinkedList
Output: green yellow red


Download ppt "Intro to OOP with Java, C. Thomas Wu List"

Similar presentations


Ads by Google