L13 Arraylists IntroGenerics A (Ch11) By4
L13 Arraylists IntroGenerics A (Ch11) By4
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 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 2
COSC 121. Page 9 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 10
Similarities
This declaration sets up an empty ArrayList The initial capacity
is set to a default value.
• Both can store a number of references
• The data can be accessed via an index. ArrayList<Date> list2 = new ArrayList<Date>(100);
Differences
list2 holds Date references
• ArrayList can automatically increase in capacity as necessary
• Will request more space from the Java run-time system, if necessary. This declaration can be used to specify the initial capacity – in
• ArrayList have methods to do many actions this case 100 elements.
• e.g. can insert/remove elements anywhere in the list • This can be more efficient in avoiding repeated work by the system in
extending the list, if you know approximately the required initial capacity.
• 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. Whenever you use array lists, you must import java.util.ArrayList.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 11 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 12
How to declare an ArrayList? Initializing an ArrayList
Since Java 7, the statement Once you create an array list, you can start adding elements to it
using its add method:
ArrayList<String> list1 = new ArrayList<String>(); ArrayList<String> list = new ArrayList< >();
can be simplified to list.add("R"); //add "R" to end of list
list.add("B"); //add "B" to end of list
ArrayList<String> list1 = new ArrayList< >();
list.add(1,"G"); //add "G" between R and G
compiler is able to infer the type from the variable declaration.
You can initialize an array list using another collection:
If Type is omitted, the default type is Object ArrayList<Type> list1 = new ArrayList<>(aCollection);
ArrayList list3 = new ArrayList(); We can use Arrays.asList method to create a fixed-size list
backed by specified elements (more about this next class).
list3 has references of the type Object, which means we can ArrayList<String> list1
store any object types in it (WHY?) = new ArrayList< >(Arrays.asList("R","G"));
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 13 COSC 121. Page 14
D. x.add(1, "C");
COSC 121. Page 21 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 22
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 23 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 24
The Stack Practice 3
A Stack represents a LIFO (last-in-first-out) data Create a custom stack class, MyStack, that can hold objects
structure. The elements are accessed only from the according to the following:
top of the stack. That is, you can only retrieve, insert,
or remove an element from the top of the stack.
A
KT
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. COSC 121. Page 25 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.size() == 0;
}
public int size() {
return list.size();
}
public void push(Object e) {
list.add(e);
}
public Object pop() {
if(list.size()>0) Sample Applications in Gaming
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 COSC 121. Page 28
Practical Example Pseudo code for a space shooter game
The following example creates an ‘army’ of robots, e.g. in a game Create an ArrayList<Star> stars
and then control all of them using a loop (e.g., move forward) Create an ArrayList<Enemy> enemies
Create an ArrayList<Bullet> bullets
ArrayList<Robot> robots = new ArrayList<>(5);
robots.add(new Robot(1,1)); In each frame{
robots.add(new Robot(2,1)); for(Star star: stars) {star.move() }
robots.add(new Robot(3,1));
for(Enemy enemy:enemies){enemy.move()}
System.out.println("BEFORE:" + robots); for(Bullet bullet: bullets) {
for(Robot r: robots) //move your robot army forward bullet.move();
r.moveForward(); for(Enemy enemy: enemies)
System.out.println("AFTER :" + robots); if (bullet.hits(enemy)){
bullets.remove(bullet);
class Robot{
private int x, y; enemies.remove(enemy);
public Robot(int x, int y) {this.x = x;this.y = y;} score++
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 COSC 121. Page 30