L13_Arraylists_IntroGenerics_A_(Ch11)
L13_Arraylists_IntroGenerics_A_(Ch11)
Part 1/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
Previous Lecture
▪ Recursion:
• How it really works
• Tail-Recursive Method
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 2
Outline
Today:
▪ Intro to Java Collection Framework
▪ The ArrayList Class
▪ Implementing a Stack using ArrayList
▪ Sample applications
Next lecture:
▪ Iterating Over an ArrayList
▪ Random Access in ArrayLists
▪ Useful Methods for Lists
• Arrays and Collections classes
Note: This diagram is not accurate. There are several abstract classes and
interfaces
Liang, omitted,
Introduction to Java butEdition,
Programming, Tenth it helps show
(c) 2015 Pearson theInc.big picture.
Education, COSC 121. Page 8
The ArrayList Class
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 10
Array vs. ArrayList
Objects of the generic class ArrayList are similar to
arrays, but have differences:
▪ Similarities
• Both can store a number of references
• The data can be accessed via an index.
▪ Differences
• ArrayList can automatically increase in capacity as necessary
• Will request more space from the Java run-time system, if necessary.
• ArrayList have methods to do many actions
• e.g. can insert/remove elements anywhere in the list
• ArrayList is not ideal in its use of memory.
• As soon as an ArrayList requires more space than its current
capacity, the system will allocate more memory space for it. The
extension is more than required for the items about to be added.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 11
How to declare an ArrayList?
ArrayList<String> list1 = new ArrayList<String>();
can be simplified to
ArrayList<String> list1 = new ArrayList< >();
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 22
Practice 2
Write a program that reads from the user a set of integers and
stores them in an ArrayList. The input ends with 0 where the
program displays all stored elements. Your list should have no
duplicates. For example:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 23
Practice 2 – cont’d
Simpler code
//read numbers
Scanner in = new Scanner(System.in);
System.out.println("Enter integers (0 to end): ");
while((value = in.nextInt()) != 0)
if(!list.contains(value))
list.add(value);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 24
The Stack
A Stack represents a LIFO (last-in-first-out) data
structure. The elements are accessed only from the
top of the stack. That is, you can only retrieve, insert,
or remove an element from the top of the stack.
push pop
KT
A
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 25
Practice 3
Create a custom stack class, MyStack, that can hold objects
according to the following:
MyStack
-list: ArrayList A list to store elements.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 26
class MyStack {
Solution private ArrayList list = new ArrayList<>();
public boolean isEmpty() {
return list.isEmpty();
}
public int size( ) {
return list.size();
}
public void push(Object e) {
list.add(e);
}
public Object pop() {
if(list.size()>0)
return list.remove(list.size() - 1);
else
return null;
}
public Object peek() {
if(list.size()>0)
return list.get(list.size() - 1);
else
return null;
}
} COSC 121. Page 27
Sample Applications in Gaming
System.out.println("BEFORE:" + robots);
for(Robot r: robots) //move your robot army forward
r.moveForward();
System.out.println("AFTER :" + robots);
class Robot{
private int x, y;
public Robot(int x, int y) {this.x = x;this.y = y;}
public void moveForward(){y++;}
public String toString() {return "("+x+","+y+")";}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 29
Pseudo code for a space shooter game
Create an ArrayList<Star> stars
Create an ArrayList<Enemy> enemies
Create an ArrayList<Bullet> bullets
In each frame{
for(Star star: stars) {star.move() }
for(Enemy enemy:enemies){enemy.move()}
for(Bullet bullet: bullets) {
bullet.move();
for(Enemy enemy: enemies)
if (bullet.hits(enemy)){
bullets.remove(bullet);
enemies.remove(enemy);
score++
}
}
}
COSC 121. Page 30