



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture is part of lecture series delivered by Narayan Singh for Java Programming course at Cochin University of Science and Technology. It includes: List, Iterator, Methods, Demo, Code, Array, List, Dynamic, Random, Access, Resized, Capacity, Objects
Typology: Slides
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























19
«interface» Collection
«interface» Iterator
«interface» ListIterator
«interface» List // The three Iterator < E > methods, plus: int nextIndex () boolean hasPrevious () E previous () int previousIndex () void add (E obj) void set (E obj)
Can traverse the list backward
Can add elements to the list (inserts after the last visited element) Can change elements (changes the last visited element)
adrish.b@ardentcollaboratio
282
284
"Cat" "Hat" "Bat"
285
ArrayList
ArrayList
287
ArrayList< E > Methods (a
Subset)
use equals to compare objects
i must be from 0 to size() - 1
inserts obj as the i -th value; i must be from 0 to size()
returns true
288
Output ArrayList ‟s toString method returns a string of all the elements, separated by commas, within [ ].
290
a 0 a 1 a 2^ ... an -
«interface» Iterator
«interface» ListIterator
«interface» List
ArrayList LinkedList
header
291
«interface» Iterator
«interface» ListIterator
«interface» List
ArrayList LinkedList
void addFirst (E obj) void addLast (E obj) E getFirst () E getLast () E removeFirst () E removeLast ()
293
public class ListNode { private Object value; private ListNode next ;
public ListNode (Object v, ListNode nx) { value = v; next = nx; }
public Object getValue ( ) { return value; } public ListNode getNext ( ) { return next; } public void setValue (Object v) { value = v; } public void setNext (ListNode nx) { next = nx; } }
A reference to the next node
Represents a node of a singly-linked list
294
public ListNode append (ListNode head, Object x) { return new ListNode (value, head); }
value 0 value 1 value 2
value ... value n -
x
head
296
Singly-Linked List — Traversal
public void printList (ListNode head) { for (ListNode node = head; node != null; node = node.getNext ()) System.out.println (node.getValue ()); }
297
value 0 value 1 value 2
value ... value n -
head tail
299
a 0 a 1 a 2^ ... an -
head
300
a 0 a 1 a 2^ ... an -
private ListNode2 header;
a field in the DoublyLinkedList class