Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)

Similar presentations


Presentation on theme: "Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)"— Presentation transcript:

1 Chapter 6 The Collections API

2 Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++) System.out.println(v[i]);

3 Custom Container 1. Implement a class MyContainer according to the UML specification given in the right. 2. Create following objects. 1. Circle c1 = new Circle(10.0); 2. Circle c2 = new Circle(5.0); 3. Rectangle r1 = new Rectangle(5,9); 4. Rectangle r2 = new Rectangle(3,10); 3. How do you add these objects into your container? MyContainer - shapes : Shape [] +add(Shape): void +remove(Shape); void +isEmpty() : boolean +clear() : void +size() : int

4 Collection Interface Major methods of Collection interface public interface Collection { 1. int size(); 2. boolean isEmpty(); 3. boolean contains(Object o); 4. Object[] toArray(); 5. Object[] toArray(Object a[]); 6. boolean add(Object o); 7. boolean remove(Object o); 8. void clear(); 9. Iterator iterator(); }

5 Iterator Interface Member methods of Iterator Interface public interface Iterator { boolean hasNext(); Object next(); void remove(); }

6 Using Iterator Interface Array Iteration Object [] v = new Object[10]; while( i< v.length ) System.out.println(v[i++]); Container Iteration Collection c ; Iterator itr =c.iterator(); while( itr.hasNext() ) System.out.println(itr.next());

7 Extending Collection List Interface List has precise control over where in the list each element is inserted. Major methods of List interface public interface List extends Collection { 1. Iterator iterator(); 2. Object get(int index); 3. Object set(int index, Object element); 4. void add(int index, Object element); 5. Object remove(int index); 6. int indexOf(Object o); 7. int lastIndexOf(Object o); 8. List subList(int fromIndex, int toIndex); 9. …………… }

8 ListIterator Member methods of ListIterator interface public interface ListIterator extends Iterator { boolean hasNext(); Object next(); boolean hasPrevious(); Object previous(); int nextIndex(); int previousIndex(); void remove(); void set(Object o); void add(Object o); }

9 Difference between Iterator & ListIterator ListIterator allows bidirectional movement. Case 1: public static void show(Collection col) { Iterator itr = col.iterator(); while( itr.hasNext() ) System.out.println(itr.next()); }

10 Difference between Iterator & ListIterator ListIterator allows bidirectional movement. Case 2: public static void showBackward(List ll) { ListIterator itr = ll.listIterator(); while( itr.hasNext()) // go to the last itr.next(); while( itr.hasPrevious() ) // print backward System.out.println(itr.previous()); }

11 Classes Implementing Collection LinkedList Stack ArrayList HashSet LinkedHashSet TreeSet Vector

12 Linked List Implements Collection and List Major methods of Stack add(Object); add(int index, Object); addFirst(Object); addLast(Object); Object getFirst(); Object getLast(); Object get(int index); ListIterator listIterator();

13 Linked List Provides methods to get, remove and insert an element at the beginning and end of the list. Can be used as a stack, queue, or double-ended queue (deque). Java LinkedList class is in fact a doubly linked list.

14 Using Linked List? public static void main(String [] args) { 1. LinkedList ll = new LinkedList(); 2. ll.add("Hello 1"); 3. ll.add("World 2"); 4. show(ll); 5. ll.addFirst("Start"); 6. ll.addLast("Last"); 7. ll.add(0,”0-th element”); 8. show(ll); }

15 Implementing LinkedList. 1. class Node { 2. Object data; 3. Node next; 4. public String toString() { 5. return data.toString(); 6. } 7. }

16 Implementing LinkedList. 1. class MyLinkedList { 2. Node head, // head of the linked list 3. tail, // tail of the linked list 4. current; // current node of the linked list 5. public MyLinkedList() { 6. // create sentinel nodes, head and tail 7. head = new Node(); 8. tail = new Node(); 9. head.next = tail; 10. tail.next = tail; 11. current = head; 12. }

17 Implementing LinkedList 1. public void add(Node n1) { 2. n1.next = current.next; 3. current.next = n1 ; 4. current = n1 ; 5. } 6. public void addFirst(Node n1) { 7. n1.next = head.next; 8. head.next = n1; 9. } 10. public Node getFirst() { 11. return head; 12. } 13. // How would you implement the following method? 14. public Node getLast() {…………..}

18 Linked List How to implement the iterator(), or listIterator() Method?

19 Implementing LinkedList 1. public MyIterator iterator() 2. { 3. return new MyIterator(head); 4. }

20 Implementing LinkedList 1. class MyIterator { 2. Node current ; 3. MyIterator(Node head) 4. { this.current = head ; } 5. boolean hasNext() { 6. boolean flag = false ; 7. Node next = current.next ; 8. if ( !next.next.equals(next) ) flag = true; 9. return flag; 10. } 11. Node next() { 12. current = current.next ; 13. return current ; 14. } 15. }

21 Stack Implements Collection and List Major methods of Stack push() pop() peek() empty()

22 Using Stack visitation 1 1. Stack mystack = new Stack(); mystack.push(new Integer(1)); 4. mystack.push(new Double(2.0)); 5. mystack.push(new String("Hello")); 6. mystack.push(new String(“Ivey”)); 7. System.out.println(mystack.pop()); 8. System.out.println(mystack.pop()); 9. System.out.println(mystack.pop());

23 Using Stack visitation 2 1. Stack mystack = new Stack(); mystack.push(new Integer(1)); 4. mystack.push(new Double(2.0)); 5. mystack.push(new String("Hello")); 6. mystack.push(new String(“Ivey”)); Iterator itr = mystack.iterator(); 9. while(itr.hasNext()) 10. System.out.println(itr.next());

24 Using Stack visitation 3 1. Stack mystack = new Stack(); mystack.push(new Integer(1)); 4. mystack.push(new Double(2.0)); 5. mystack.push(new String("Hello")); 6. mystack.push(new String(“Ivey”)); ListIterator litr = mystack.listIterator(); 9. System.out.println(litr.next()); 10. System.out.println(litr.previous());

25 Implementation of Stack 1 Array Version 1. class MyStack{ 2. int currentIndex ; 3. int maxSize = 100; 4. Object [] objectlist = new Object[maxSize]; 5. MyStack() 6. { currentIndex = 0 ; } 7. void push(Object obj) 8. { 9. objectlist[currentIndex] = obj ; 10. currentIndex++; 11. } 12. // implements all the methods of Collection interface 13. ………… ………… }

26 Implementation of Stack 2 Linked List Version 1. class MyStack{ 2. int currentIndex ; 3. int maxSize = 100; 4. LinkedList objectlist ; 5. MyStack() 6. { objectlist = new LinkedList() } 7. void push(Object obj) 8. { 9. objectlist.addLast(obj) ; 10. } 11. // implements all the methods of Collection interface 12. ………… ………… }

27 Implementation of Stack 2 How to implement the iterator(), or listIterator() Method?


Download ppt "Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)"

Similar presentations


Ads by Google