This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others. CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
ArrayList<String> myArrayList = new ArrayList<>();
11
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<>();
import java.util.*;
12
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<>();
13
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<>();
14
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<>();
Type of items your ArrayList will store.
15
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<>();
16
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<>();
17
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<>();
18
Our First ArrayList// Create an (initially empty) listArrayList<String> list = new ArrayList<>();
19
Our First ArrayList// Create an (initially empty) listArrayList<String> list = new ArrayList<>();
// Add an element to the backlist.add("Hello"); // now size 1
“Hello”
20
Our First ArrayList// Create an (initially empty) listArrayList<String> list = new ArrayList<>();
// Add an element to the backlist.add("Hello"); // now size 1
list.add("there!"); // now size 2
“Hello”
“Hello” “there!”
21
Our First ArrayList// Add an element to the backlist.add("Hello"); // now size 1
list.add("there!"); // now size 2
// Access elements by index (starting at 0!)println(list.get(0)); // prints "Hello"println(list.get(1)); // prints "there!”println(list); // prints ["Hello", "there!"]
“Hello”
“Hello” “there!”
22
Our First ArrayList// Add an element to the backlist.add("Hello"); // now size 1
list.add("there!"); // now size 2
// Access elements by index (starting at 0!)for (int i = 0; i < list.size(); i++) {println(list.get(i));
}
“Hello”
“Hello” “there!”
23
Our First ArrayList// Add an element to the backlist.add("Hello"); // now size 1
list.add("there!"); // now size 2
// Access elements by index (starting at 0!)for (int i = 0; i < list.size(); i++) {println(list.get(i));
}
“Hello”
“Hello” “there!”
24
Our First ArrayList// Add an element to the backlist.add("Hello"); // now size 1
list.add("there!"); // now size 2
// Access elements in order (also for arrays!)for (String str : list) {println(str);
}
“Hello”
“Hello” “there!”
25
Iterating Over ArrayLists// Access elements in order (also for arrays!)for (String str : list) {println(str);
}
// equivalent to
for (int i = 0; i < list.size(); i++) {String str = list.get(i);println(str);
}
26
Iterating Over ArrayLists// Access elements in order (also for arrays!)for (String str : list) {println(str);
}
// equivalent to
for (int i = 0; i < list.size(); i++) {String str = list.get(i);println(str);
}
27
Bad Times with ArrayLists// Create an (initially empty) listArrayList<String> list = new ArrayList<>();
// Wrong type – bad times! Won’t compileGLabel label = new GLabel("Hello there!");list.add(label);
"I Have a Dream" by Antonia Lee, Sara Fung, Christy Fung, Rachel Lamhttp://poets.spice.org.hk/index.php?option=com_content&view=article&id=45:my-family&catid=6:reverse-poem&Itemid=7
31
Example: Reversible WritingLet’s write a program that reverses a text file.
“Iamnotapersonwhocontributes”
32
Example: Reversible WritingLet’s write a program that reverses a text file.