Week2 1 Java
Week2 1 Java
ArrayList, HashMap
Memory
Let's Talk About Memory
• Random Access Memory (RAM)
• Access any value by index
• E ectively, a giant array
• All values in your program are stored here
ff
Let's Talk About Memory
• Operating System (OS) Stack Memory
controls memory ...
•
public static void main(String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
Sequential data structure
[Link](10-x);
}
[Link](arr1);
ArrayList<Integer> arr2 = arr1;
• Order matters
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}
• Values indexed starting at 0
Java - ArrayList
package week2; • ArrayList is built-in with Java
import [Link];
•
for (int x=0; x<[Link](); x++) {
}
out += [Link](x); Unlike String, int, double, etc.
return out;
}
•
public static void main(String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
Must import ArrayList
•
[Link](10-x);
}
[Link](arr1);
The ArrayList class is in the
ArrayList<Integer> arr2 = arr1;
[Link](arr2);
[Link] package
int total = sum(arr1);
}
[Link]("total: " + total);
• Importing makes the class
}
available in your code
Java - ArrayList
package week2; • Use the "new" keyword to create a
import [Link]; new ArrayList
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) { • Must have <> which is a type
int out = 0;
for (int x=0; x<[Link](); x++) {
parameter list
out += [Link](x);
}
return out;
• Can also have <Integer> in this
} example
•
public static void main(String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>(); Must have () which is an empty
for (int x=0; x<4; x++) {
[Link](10-x); argument list
}
[Link](arr1);
ArrayList<Integer> arr2 = arr1; • This calls the classes constructor
[Link](arr2);
int total = sum(arr1); method and returns an object
[Link]("total: " + total);
}
}
• Much more detail to come in week 4
Java - ArrayList
package week2;
• An ArrayList variable should
import [Link];
have a type parameter in <>
public class ArrayList1 {
•
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
for (int x=0; x<[Link](); x++) {
This ArrayList has a type
}
out += [Link](x);
parameter of Integer
return out;
•
}
}
}
return out; • Class types start with capital
public static void main(String[] args) {
letters
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
}
[Link](10-x);
• You cannot create an
[Link](arr1);
ArrayList<Integer> arr2 = arr1; ArrayList of ints
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
}
}
Java - ArrayList
package week2; • int ≈ Integer
import [Link];
• double ≈ Double
•
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) { boolean ≈ Boolean
int out = 0;
for (int x=0; x<[Link](); x++) {
out += [Link](x); • Use the class equivalents for our
}
return out;
primitive (starts with lowercase letter)
} types
•
public static void main(String[] args) {
ArrayList<Integer> arr1 = new ArrayList<>(); [In most cases] Java will automatically
for (int x=0; x<4; x++) {
[Link](10-x); convert between the two
}
[Link](arr1);
ArrayList<Integer> arr2 = arr1; • Conversion is called auto-boxing
[Link](arr2);
int total = sum(arr1);
[Link]("total: " + total);
• We'll always use the primitive types
} unless we must use the class equivalent
}
Java - ArrayList
package week2;
• Call the add method to insert a
import [Link];
value at the end of the
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) { ArrayList
int out = 0;
for (int x=0; x<[Link](); x++) {
out += [Link](x);
}
return out;
}
• We create an ArrayList
• ArrayLists go in the heap!
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1
for (int x=0; x<[Link](); x++) {
out += [Link](x);
}
return out;
}
• When a variable is assigned a value that is a reference, only the reference is assigned!
• There is no copy of the ArrayList created. Only 1 ArrayList exists in memory
• That ArrayList is referred to by the 2 variables that store its reference
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2); [10, 9, 8, 7]
int total = sum(arr1);
[Link]("total: " + total);
}
}
• This is **assign-by-reference**
• Only the reference is assigned
Technically it's assign-by-value, but the value is a reference
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
}
[Link](arr1); in/out
ArrayList<Integer> arr2 = arr1;
[Link](arr2); [10, 9, 8, 7]
int total = sum(arr1);
[Link]("total: " + total);
[10, 9, 8, 7]
}
}
• arr2 refers to the same ArrayList as arr1 -- the only ArrayList in this
example
• Printing arr2 is the same as printing arr1
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 [10, 9, 8, 7]
int total = sum(arr1);
[Link]("total: " + total);
[10, 9, 8, 7]
}
}
• When a method is called that take an object on the heap as a parameter, only the reference is
passed into the stack frame
• This is **pass-by-reference**
Technically it's pass-by-value, but the value passed is a reference
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 [10, 9, 8, 7]
int total = sum(arr1);
[10, 9, 8, 7]
}
[Link]("total: " + total); x 0
}
• When using the reference, the dot operator . means we follow the reference to the
object to which it refers
• [Link]() means - go to the ArrayList referred to by this reference and call it's size
method
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 10 [10, 9, 8, 7]
int total = sum(arr1);
[10, 9, 8, 7]
}
[Link]("total: " + total); x 0
}
• [Link](x)
• Follow the reference
• Return the value stored at index x and add 10 to the out variable
package week2; Stack
import [Link]; Name Value
Heap
public class ArrayList1 {
public static int sum(ArrayList<Integer> arrIn) {
int out = 0;
arr1 0x200 0x200
for (int x=0; x<[Link](); x++) {
out += [Link](x); 0 10
} x 0 1 2 3 4 1 9
return out;
} 2 8
public static void main(String[] args) { arr2 0x200 3 7
ArrayList<Integer> arr1 = new ArrayList<>();
for (int x=0; x<4; x++) {
[Link](10-x);
total
}
in/out
[Link](arr1);
ArrayList<Integer> arr2 = arr1; arrIn 0x200
[Link](arr2);
sum out 0 10 19 27 34 [10, 9, 8, 7]
int total = sum(arr1);
[10, 9, 8, 7]
}
[Link]("total: " + total); x 0 1 2 3 4
}
• Print total
• End of program
HashMap
Java - HashMap
package week2; • Similar to:
import [Link];
}
}
}
• Used to associate keys with values
Java - HashMap
package week2;
import [Link];
• Must import before use
public class HashMap1 {
public static void main(String[] args) {
•
HashMap<String, Integer> bills = new HashMap<>();
•
[Link](key);
}
for (Integer value : [Link]()) {
We say this is a:
[Link](value);
}
for (String key : [Link]()) { • HashMap from String to
int value = [Link](key);
[Link](key + "'s number is: "); Integer
[Link](value);
}
}
}
• Maps Strings to Integers
Java - HashMap
package week2;
• Add key-value pairs using
import [Link];
"put"
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>();
[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
[Link]([Link]("Allen"));
• Retrieve a value at a
[Link](bills);
for (String key : [Link]()) { particular key using "get"
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value);
}
}
}
Java - HashMap
package week2;
import [Link];
• for-each loop
public class HashMap1 {
public static void main(String[] args) {
• Or "enhanced" loop in the
HashMap<String, Integer> bills = new HashMap<>();
world of Java
[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
[Link]([Link]("Allen"));
•
[Link](bills);
for (String key : [Link]()) {
[Link](key); Very similar to Python loops
}
for (Integer value : [Link]()) {
[Link](value); for (type variableName : dataStructure)
}
•
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
Read: for variableName in
}
}
[Link](value);
dataStructure
}
Doesn't have to be a data structure.
Anything that can be iterated over will work
Java - HashMap
package week2; • keySet
•
import [Link];
•
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
Common to iterate over the keys
}
[Link](value);
and access the values if you
}
}
need both
Memory Diagram
Coleman=0}
Coleman
“Coleman” 0
“Coleman”
Coleman 0
0
“Coleman”
0
package week2;
Stack
import [Link]; Name Value
Heap
public class HashMap1 {
public static void main(String[] args) {
HashMap<String, Integer> bills = new HashMap<>(); bills 0x519 0x519
[Link]("Allen", 17);
[Link]("Coleman", 0);
[Link]("What is Allen's number? ");
[Link]([Link]("Allen"));
[Link](bills);
for (String key : [Link]()) {
[Link](key);
}
for (Integer value : [Link]()) {
[Link](value);
}
for (String key : [Link]()) {
int value = [Link](key);
[Link](key + "'s number is: ");
[Link](value); in/out
}
}
}
Coleman
“Coleman” 0
“Coleman”
Coleman 0
0
“Coleman”