Commit f39ec133 authored by michael lundquist's avatar michael lundquist
Browse files

starting notes in ch 3

parent 11ba4688
......@@ -357,7 +357,7 @@ String narrowerObj = (String) tempStr;//can be re-narrowed explicitly
### 2.5.2 Generics
- Java supports __generic__ (as of Java SE 5) classes and methods that take a set of __formal type parameters__ upon object initialization and use them in class as variable, parameter, and return types in the object.
- The __composition design pattern__ deals the instance variables of a class.
- The __composition design pattern__ deals with the instance variables of a class.
- Before generics, java programmers relied heavily on the Object superclass to make their code reusable. Unfortunately, this also makes code less robust as it introduces a need for narrowing casts back from the Object type.
#### Using Java's Generics Framework
......@@ -367,7 +367,7 @@ String narrowerObj = (String) tempStr;//can be re-narrowed explicitly
- Note, generics only works with Object types, not base types. However, this is rarely a problem because boxing and unboxing.
- __type inference__: an object's actual type parameters can be inferred from the actual type parameters of the variable they're being assigned to using empty angle brackets known as the "diamond" `<>`.
- These type parameters can also be explicitly stated inside the anble brackets.
- These type parameters can also be explicitly stated inside the angle brackets.
- However, if you don't have any angle brackets, all the type parameters are set to Object.
# CH3 Fundamental Data Structures
## 3.1 Using Arrays
- arrays use integer indicies.
- here we explore applications of arrays
### 3.1.1 Sorting Game Entries in an Array
- sorting elements in an array requires shifting elements smaller than that element to the right.
- in exercise C-3.19, we implement add and remove from a non-sorted array in O(1)
- ArrayLists are discussed in section 7.2. Basically ArrayLists are arrays that resize when they have to.
### 3.1.2 Sorting an array
- This section covers Insertion-Sort
#### The Insertion-Sort Algorithm
- ch 12 covers sorting in-depth. This section covers insertion-sort.
- __Insertion sort__: sorting by iteratively inserting the first element from the unsorted half at the correct position in the unsorted half.
- This obviously requires shifting the other elements right.
### 3.1.3 Java.util Methods for Arrays with Random Numbers
- `java.util.Arrays` provides the following methods
- `equals(A, B)` the arrays A and B have the same elements in the same order.
- `fill(A, x)` stores *x* in every cell in array *A*, provided *A* is of a type that can store *x*.
- `copyOf(A, n)` returns an array of size *n* with the first *n* elements copied from *A* into the new array (or all of A copied into the first n elements of the new array)
- `copyOfRange(A, s, t)` Returns an array of size *t-s* such that the elements of this array are copied in order from *A[s]* to *A[t-1]*, where *s<t*, padded as with `copyOf()` if *t > A.length*
- `toString(A)` returns a String representation of the array *A*, beginning with [, and ending with ], and with elements of *A* displayed separated by string ", ". The string representation of an element *A[i]* is obtained using `String.valueOf(A[i])`, which returns the string "null" for a __null__ reference and othersize calls `A[i].toString()`.
- `sort(A)` Sorts the array *A* based on a natural ordering of its elements, which must be comparable. Sorting algorithms are the focus of Chapter 12
- `binarySearch(A, x)` searches the *sorted* array A for value x, returning the index from where it's found, or else the index of where it could be inserted while maintaining the sorted order. The binary-search algorithm is described in Section 5.1.3.
#### Pseudo Random Number Generation
- Random number generators are good for providing randomized test data.
- These random number generators use __pseudorandom numbers__ which are iteratively generated from a __seed__ using a formula.
- java uses the formula $next = (a * cur + b) % n;$ to generate random numbers. If you choose *a*, *b*, and *n* right, numbers are evenly distributed. Java uses $2^{48}$ as its n.
- As we've shown, pseudorandom numbers are predictable, and shouldn't be used for secure applications. If you need truly random numbers, use a truly random source, like radio white-noise.
- common real-world truly random numbers: time (since epoch using `System.currentTimeMillis`, for user to respond to a set seed prompt) and (x,y) of the computer mouse.
- note, these aren't the only possibilities.
- You can make a semi-random number that's much faster than a truly random number by initializing a pseudorandom number with a truly random seed using `java.util.Random.setSeed()` or using the `java.util.Random` constructor and passing the seed parameter.
- `java.util.Random` methods:
- `nextBoolean()`
\ No newline at end of file
import java.util.Arrays;
public class Examples{
public static void main(String[] args){
* Exercise C.19 scoreboard of ints
public static class ScoreBoard{
private int[] scoreboard;
private int numbElems = 0;
public ScoreBoard(int size){
this.scoreboard = new int[size];
public void add(int score){
scoreboard[numbElems] = score;
if(numbElems < scoreboard.length - 1 ){
numbElems ++;
returns the element at the index
public int remove(int index){
if(index < numbElems){
int tmp = scoreboard[index];
scoreboard[index] = scoreboard[this.numbElems - 1];
numbElems --;
return tmp;
return null;
public static void test(){
ScoreBoard board = new ScoreBoard(5);
for(int i = 0; i < 10; i++){
public String toString(){
return Arrays.toString(scoreboard);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment