Programming Java Part9-Java Programming-Lecture Slides, Slides of Java Programming

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

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

96 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
19
ListIterator<E>
Methods «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)
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Programming Java Part9-Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

19

ListIterator < E >

Methods

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

Demo Code

 List list = ...;

 ListIterator iterator =

list.listIterator(list.size());

 while (iterator.hasPrevious()) {

 Object element = iterator.previous();

 // Process element

adrish.b@ardentcollaboratio

282

284

java.util.ArrayList< E >

 Implements a list using an array.

 Can only hold objects (of a specified type), not

elements of primitive data types.

 Keeps track of the list capacity (the length of the

allocated array) and list size (the number of

elements currently in the list)

"Cat" "Hat" "Bat"

capacity

size

285

ArrayList  Generics

 Starting with Java 5, ArrayList and other

collection classes hold objects of a specified data

type.

 The elements‘ data type is shown in angle

brackets and becomes part of the ArrayList type.

For example:

ArrayList words = new ArrayList ();

ArrayList nums = new ArrayList ();

287

ArrayList< E > Methods (a

Subset)

int size ()

boolean isEmpty ()

boolean add (E obj)

void add (int i, E obj)

E set (int i, E obj)

E get (int i)

E remove (int i)

boolean contains (E obj)

int indexOf (E obj)

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

ArrayList Example

ArrayList names =

new ArrayList( );

names.add("Ben");

names.add("Cat");

names.add(0, "Amy");

System.out.println(names);

[Amy, Ben, Cat]

Output ArrayList ‟s toString method returns a string of all the elements, separated by commas, within [ ].

290

LinkedList

 Represents a list as a doubly-linked list with a

header node

 Implements all the methods of List < E >

a 0 a 1 a 2^ ... an -

«interface» Iterator

«interface» ListIterator

«interface» List

ArrayList LinkedList

header

291

LinkedList

(cont‘d)

 Additional methods specific to LinkedList:

«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

Singly-Linked List (cont‘d)

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

Singly-Linked List — Example 1

 Append x at the head of a linked list and return

the head of the new list.

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

Singly-Linked List with a Tail

 Keeps a reference to the last node

 Suitable for implementing a queue

value 0 value 1 value 2

value ... value n -

head tail

299

Doubly-Linked Circular List

 next in the last node points to the first node

 previous in the first node points to the last node

a 0 a 1 a 2^ ... an -

head

300

Doubly-Linked Circular List with a

Header Node

 That‘s how java.util.LinkedList is implemented

a 0 a 1 a 2^ ... an -

private ListNode2 header;

a field in the DoublyLinkedList class