L14 Arraylists IntroGenerics B (Ch11)
L14 Arraylists IntroGenerics B (Ch11)
Part 2/2
Acknowledgement: The slides mainly rely on the textbook of Y. D. Liang titled “Introduction to
Java Programming, 10th Ed.”, Pearson Edu. Inc. COSC 121. Page 1
Outline
Previously:
▪ Intro to Java Collection Framework
▪ The ArrayList Class
▪ Implementing a Stack using ArrayList
▪ Sample applications
Today:
▪ Iterating Over an ArrayList
▪ Random Access in ArrayLists
▪ Useful Methods for Lists
• Arrays and Collections classes
Using for-each
Using an iterator.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 4
Iterators and ArrayList
An ArrayList (and each collection) is Iterable. You can
obtain its Iterator to traverse all its elements.
Iterator Iterator
0 1 2 3 0 1 2
remove()
Iterator Iterator
Iterators and ArrayList, cont.
listIterator(): returns a ListIterator object
▪ ListIterator is a subtype of Iterator
▪ Methods:
• next(), previous()
• hasNext(), hasPrevious()
• remove()
• nextIndex(), previousIndex() returns the next/previous index
0 1 2 3
ListIterator
previous() next()
hasPrevious() hasNext()
previousIndex() nextIndex()
COSC 121. Page 7
Iterators and ArrayList, cont.
listIterator() also allows initializing the iterator with an
index
▪ To position the iterator some place other than the beginning of
the list.
0 1 2 3
x y z
it.add("X"); A X B C D
it
it.add("Y"); A X Y B C D
it
it.previous(); A X Y B C D
it
it.set("Z"); A X Z B C D
it
It.set("W"); A X W B C D
it COSC 121. Page 9
Practice 4
Find the sum of all elements in array list, nums, using three
different ways (loops).
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4));
//using iterator
sum = 0;
Iterator<Integer> it = list.iterator();
while(it.hasNext())
sum += it.next();
COSC 121. Page 10
Practice 5
Use a ListIterator to print all elements in an ArrayList in
forward direction then in backward direction.
▪ e.g., for [3, 2, 6, 9], the output would be:
Elements in forward direction: 3 2 6 9
Elements in backward direction: 9 6 2 3
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(3,2,6,9));
ListIterator<Integer> it = list.listIterator();
//forward printing
while(it.hasNext())
System.out.print(it.next() + " ");
System.out.println();
//backward printing
while(it.hasPrevious())
System.out.print(it.previous() + " ");
COSC 121. Page 11
Practice 6
Use a ListIterator to print all elements in an ArrayList in
backward direction.
▪ e.g., for [3, 2, 6, 9], the output would be 9 6 2 3
ListIterator<Integer> it = list.listIterator(list.size());
//backward printing
while(it.hasPrevious())
System.out.print(it.previous() + " ");
Efficient?
Solution 1:
while(x.contains("A"))
x.remove("A");
Solution 2: Efficient?
ArrayList → array
• Creating an array from an ArrayList:
• Syntax: list.toArray(array);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 21
java.util.Collections methods
Collections.sort (if elements are comparable):
Collections.min or .max
Collections.shuffle
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 22
Intro to Generics
Examples:
list.add(5.5); // 5.5 is automatically converted to new Double(5.5)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 31
Defining Generic Classes and Interfaces
You have seen before how to create MyStack class that holds
instances of the type Object, which means any type (you can
store cars, apples, and humans in the same stack).
GenericStack<E>
-list: ArrayList<E> A list to store elements.
+GenericStack() Generates an empty stack
+getSize(): int Returns the number of elements in this stack.
+peek(): E Returns the top element in this stack.
+pop(): E Returns and removes the top element in this stack.
+push(o: E): void Adds a new element to the top of this stack.
+isEmpty(): boolean Returns true if this stack is empty.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 33
Defining Generic Classes and Interfaces
public class MyStack<E> {
private ArrayList<E> list = new ArrayList<>();