Data Structure: an arrangement of data in a computer's memory or hard disk
Algorithm: The way of manipulating the data stored in the memory
fencepost and initial test are bros
Reference Semantics
!!!!Basics
Value Semantics (Value Types)
primitive
Reference Semantics (Reference Types)
stores address
objects
!!Why
Efficiency
memory
Share
allow diff part share an obj
Multiple Objects
list1=list2;
both refer to a same location
pass list1 to data in method
both refer to a same location
we copy the address, so when modifying it, we do not modify the copy as primitive type. we modify the value in this reference.
null
no object
Advanced Array Techniques
Shifting Values in an Array
left shifting
local variable
avoid off-by-one
right shifting
local variable
avoid over write
loop in reverse order
Arrays of Objects
two-step process
???kan bu dong
Command-Line Arguments
invoke this when java init
??? kan bu dong
Nested Loop Algorithms
1. sequence; after; length-1
2. start after outer loop; j=i+1
Multidimensional Arrays
basics
Multidimensional Array
Rectangular 2D Arrays
double[][] temps = new double [rows][columns]
grid.length; grid[i].length
System.out.println(Arrays.deepToString(temps));
row----column||||
Jagged Arrays
no. of cols varies
int[][] jagged = new int[3][];
jagged[0] = new int[2];
jagged[1] = new int[4];
jagged[2] = new int[3];
Pascal's Triangle
Array-Traversal Algorithms
Printing an array
Array.toString(<array>);
for-each
fencepost traversal loop + init test
Searching and Replacing
return -1; do not find in the list
Testing for Equality
Arrays.equals(l1, l2);
Common pattern for equals:
1. test all of the ways that cause false
2. return true at the very end
Reversing Arrays
String Traversal Algorithms
for(int i=0; i<<string>.length(); i++) {<do sth with string.charAt(i)>}
Array Basics
basics
Array
indexed
multiple values(elements) with same type
Index
Zero-based indexing
Constructing and Traversing an Array
Auto initialization
REFERENCE
Array Traversal
for loop
off-by-one error
standard pattern
Accessing an Array
Buffer overruns
fast random access
code
constructor: <element type>[] name = new <element type>[<length>]
<array>.length
last value is .length-1
middle: .length/2
Arrays and Methods
The value passed to method can be changed
do not need to return an array
return an array is better for understanding
for-each loop
for (<type> <name> : <array>) {}
simply examine each value
changing <name> does not change array
not convenient most times
standard pattern
initialize Arrays
<element type>[] <name> = {<value>, <value>...};
this way, java counts the no. and construct the right size
only 'array' and 'string' can construct obj without new
The array class
import java.util.*;(package)
limitations
cannot change the size in the middle of exe
larger array? construct a new one and copy the old one
int[] newData = Arrays.copyOf(data, 2 * data.length);
copy portion?
Array.copyOfRange
cannot print array
Arrays.toString(<array>)
cannot using == compare arrays
Array.equals(d1,d2);