Scope, Static, Linked Lists, Arrays
Discussion 03
CS 61B Fall 2023
Example Agenda
CS 61B Fall 2023
Announcements
CS 61B Fall 2023
Content Review
CS 61B Fall 2023
GRoE: Golden Rule of Equals
“Given variables y and x:
y = x copies all the bits from x into y.”
Java is pass-by-value: when you call a function and give it some arguments, the function called receives an exact copy of those arguments, tied to its own local variables.
“Copies all the bits” means different things for primitive vs. reference types.
CS 61B Fall 2023
Primitive vs. Reference Types
Examples: byte, short, int, long, float, double, boolean, char
Examples: Strings, Arrays, Linked Lists, Dogs, etc.
CS 61B Fall 2023
Back to the GRoE
“Given variables y and x:
y = x copies all the bits from x into y.”
CS 61B Fall 2023
A Quick Example
int x = 5;
int[] arr = new int[]{1, 2, 3, 5};
1
2
3
5
5
x
arr
CS 61B Fall 2023
A Quick Example
int x = 5;
int[] arr = new int[]{1, 2, 3, 5};
doSomething(x, arr);
...
public void doSomething(int y, int[] other) {
y = 9;
other[2] = 4;
}
1
2
3
5
5
x
arr
5
y
other
CS 61B Fall 2023
A Quick Example
int x = 5;
int[] arr = new int[]{1, 2, 3, 5};
doSomething(x, arr);
...
public void doSomething(int y, int[] other) {
y = 9;
other[2] = 4;
}
1
2
4
5
5
x
arr
9
y
other
CS 61B Fall 2023
Static vs. Instance, Revisited
Static variables and functions belong to the whole class.
Example: Every 61B Student shares the same professor, and if the professor were to change it would change for everyone.
Instance variables and functions belong to each individual instance.
Example: Each 61B Student has their own ID number, and changing a student’s ID number doesn’t change anything for any other student.
CS 61B Fall 2023
this vs. static
CS 61B Fall 2023
Arrays
Arrays are data structures that can only hold elements of the same (primitive or reference) type of value.
arr[i] holds a value in the ith position of the array (zero-indexed). We can also have n-dimensional
arrays (ie. int[][] a = new int[3][2]; you can index into these like a[2][1])
4
1
8
0
1
2
0
1
2
Cat
2
id
5
age
Cat
4
id
9
age
Cat
8
id
1
age
Arrays have a set length when instantiated, so they cannot be extended / shortened with pointers like a Linked List. To resize, we need to copy over all elements to a new array (ie. System.arraycopy)
CS 61B Fall 2023
Linked Lists
Linked Lists are modular lists that are made up of nodes that each contain a value and a pointer to the next node. To access values in a Linked List, you must use dot notation.
Example: intList.get(2)
CS 61B Fall 2023
Worksheet
CS 61B Fall 2023
1 Static Electricity
Write what would be printed after the main method is executed.
Java visualizer: https://ktinyurl.com/48uk72kc
CS 61B Fall 2023
1 Static Electricity
Party size: 2
Pikachu 17 Ash
Pikachu 18 Team Rocket
Pikachu 18 Cynthia
Java visualizer: https://tinyurl.com/48uk72kc
CS 61B Fall 2023
1 Static Electricity
On line 28, is level:
CS 61B Fall 2023
1 Static Electricity
On line 28, is level:
CS 61B Fall 2023
1 Static Electricity
If we were to call Pokemon.printStats() at the end of our main method, what would happen?
CS 61B Fall 2023
1 Static Electricity
Error!
CS 61B Fall 2023
2 Rotate Extra
Implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, without modifying A. Note: the modulo of a negative number is still negative
public static int[] rotate(int[] A, int k) {
int rightShift = _______________________________;
if (_________________________) {
_____________________________________________;
}
int[] newArr = ____________________________________;
for (__________________________________________) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61B Fall 2023
2 Rotate Extra
Implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, without modifying A. Note: the modulo of a negative number is still negative
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (_________________________) {
_____________________________________________;
}
int[] newArr = ____________________________________;
for (__________________________________________) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61B Fall 2023
2 Rotate Extra
Implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, without modifying A. Note: the modulo of a negative number is still negative
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;;
}
int[] newArr = ____________________________________;
for (__________________________________________) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61B Fall 2023
2 Rotate Extra
Implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, without modifying A. Note: the modulo of a negative number is still negative
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = new int[A.length];
for (__________________________________________) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61B Fall 2023
2 Rotate Extra
Implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, without modifying A. Note: the modulo of a negative number is still negative
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = new int[A.length];
for (int i = 0; i < A.length; i++) {
int newIndex = ________________________________;
_____________________________________________;
}
return newArr;
}
CS 61B Fall 2023
2 Rotate Extra
Implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, without modifying A. Note: the modulo of a negative number is still negative
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = new int[A.length];
for (int i = 0; i < A.length; i++) {
int newIndex = (i + rightShift) % A.length;
_____________________________________________;
}
return newArr;
}
CS 61B Fall 2023
2 Rotate Extra
Implement rotate such that it returns a new array containing the elements in A have shifted k positions to the right, without modifying A. Note: the modulo of a negative number is still negative
public static int[] rotate(int[] A, int k) {
int rightShift = k % A.length;
if (rightShift < 0) {
rightShift += A.length;
}
int[] newArr = new int[A.length];
for (int i = 0; i < A.length; i++) {
int newIndex = (i + rightShift) % A.length;
newArr[newIndex] = A[i];
}
return newArr;
}
CS 61B Fall 2023
3 Cardinal Directions
Draw out the resulting diagram after executing all the lines.
CS 61B Fall 2023
3 Cardinal Directions
DLLStringNode L = new DLLStringNode(null, "eat", null);
“eat”
L
prev
next
CS 61B Fall 2023
3 Cardinal Directions
L = new DLLStringNode(null, "bananas", L);
“bananas”
“eat”
L
CS 61B Fall 2023
3 Cardinal Directions
L = new DLLStringNode(null, "never", L);
“never”
“bananas”
L
“eat”
CS 61B Fall 2023
3 Cardinal Directions
L = new DLLStringNode(null, "sometimes", L);
“sometimes”
“never”
L
“bananas”
“eat”
CS 61B Fall 2023
3 Cardinal Directions
DLLStringNode M = L.next;
“sometimes”
“never”
L
“bananas”
“eat”
M
CS 61B Fall 2023
3 Cardinal Directions
DLLStringNode R = new DLLStringNode(null, "shredded", null);
“sometimes”
“never”
L
“bananas”
“eat”
M
R
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
R = new DLLStringNode(null, "wheat", R);
“sometimes”
“never”
L
“bananas”
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
R.next.next = R;
“sometimes”
“never”
L
“bananas”
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
M.next.next.next = R.next;
“sometimes”
“never”
L
“bananas”
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
L.next.next = L.next.next.next;
“sometimes”
“never”
L
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
“sometimes”
“never”
L
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
L = M.next;
“never”
L
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
M.next.next.prev = R;
“never”
L
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
L.prev = M;
“never”
L
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
L.next.prev = L;
“never”
L
“eat”
M
R
“wheat”
“shredded”
CS 61B Fall 2023
3 Cardinal Directions
R.prev = L.next.next;
“never”
L
“eat”
M
R
“wheat”
“shredded”
Java visualizer: https://tinyurl.com/3hy6n934
CS 61B Fall 2023
4 Gridify
CS 61B Fall 2023
4 Gridify
CS 61B Fall 2023
4 Gridify
CS 61B Fall 2023
4 Gridify
CS 61B Fall 2023
4 Gridify
CS 61B Fall 2023
4 Gridify
CS 61B Fall 2023
4 Gridify
CS 61B Fall 2023
4 Gridify
CS 61B Fall 2023
4 Gridify
Why helper method here? Why can’t we just have the signature for gridify also have a pointer to the curr node, such that the user of the function passes in the sentinel each time?
CS 61B Fall 2023
4 Gridify
Why helper method here? Why can’t we just have the signature for gridify also have a pointer to the curr node, such that the user of the function passes in the sentinel each time?
We need a helper to keep track of which node and index we’re on.
If we make the change: it breaks the abstraction barrier - requires our user to understand sentinels.
If they pass in random values - incorrect answer.
CS 61B Fall 2023