Java 数据结构课程,因为懒所以荒废了很久。。
讲课十分有趣清晰,老师很逗( •̀ ω •́ )✧

1.2 Defining and Using Class

javac: compile the java file to class file
java : run the compiled program

why make a class file?

  • type check
  • ‘simpler’ to execute, distributed code is faster
  • protects your intellectual, NO need to give out source code

for every single dog make a new class ?

classe can contain not just functions, but also data


static method and instant method

some classes never instantiated like **math** class i = 0 bark i = 1 woof i = 2 woof i = 3 ? not initialized

i = 3 java.lang.NullPointerException
i=3 is not a dog at all

public static void main(String[] args)

command ling argument

Exercise

using libraries

amazing and cool thing of being a programmer

2.1References Recursion and Lists

In class Dog

many instance variables
static variables every dog shares this variable
constructor determine how to instantiate the class Dog in launcher , looks like a method(but its not)
instance method : the method is going to be invoked by a instance variable in this class, then it should be instance(non-static)

In LaunchDog

declaration : declares a variable belongs to a specific class
instantiation : create a new dog
assignment : assign the new dog to a variable

Variables in Java

~~NO?~~ YES

a and b are like pointers

NO? NO! simply changing the values y copies x

types

tells how to interpret these bits in the memory

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

declaration and assignment

get boxes and filled up with certain bits

Reference type

class instantiation

**new** : find bits(size of the types in the class) "return" the address

Declaration of reference type

(a pointer :64bits)


reference type works like an arrow

List

1
2
3
4
Dog[] dogs = new Dog[2]   //create 2 dog houses
dogs[0] = new Dog(8); //create a new dog in dog house 0
dogs[1] = new Dog(20);
dogs[0].MakeNoise();

Parameter Passing

create a local variable and store the parameter in their scope
called pass by value (always copy the bit)

Declaration and Instantiation of Arrays

1
2
Planet p = new APlanet(0,0,0);
int[] x = new int[]{-,1,2,97,4}; //Declaration Instantiation and assignment
1
2
int[] a  //Declaration (create a new box, no object is insrantiated)
new int[]{-,1,2,97,4}; //Instantiation (we may lost it if it's not assigned)

Int List

1
2
3
4
5
6
7
8
9
10
11
12
13
public class IntList {
    public int first;
    public IntList rest;
    public static void main(String[] args) {
        IntList L = new IntList();
        L.first = 5;
        L.rest = null;
        L.rest = new IntList();
        L.rest.first = 10;
        L.rest.rest = new IntList();
        L.rest.rest.first = 15;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public class IntList {
    public int first;
    public IntList rest;
    public IntList(int f, IntList r) {
        first = f;
        rest = r;
    }
    public static void main(String[] args) {
        IntList L = new IntList(15, null); //the tail of the list
        L = new IntList(10, L);
        L = new IntList(20, L);
    }
}

add methods

1.size

1
2
3
4
5
6
    public int size() {
        if(rest == null) {  //in recursion we should always consider the basic case
            return 1;
        }
        return 1 + this.rest.size();
    }

```

1
2
3
4
5
6
7
8
9
10
  //return the size without using recursion
    public int iterativeSize() {
        IntList p = this;
        int totalSize = 0;
        while(p != null) {
            totalSize += 1;
            p = p.rest;
        }
        return totalSize;
    }

2.get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    public int iterativeGet(int i) {
        IntList p = this;
        int current = 0;
        while(current < i) {
            current +=1;
            p = p.rest;
        }
        return p.first;
    }
   
    public int Get(int i) {  //recursive version
        if(i == 0) {
            return first;
            return rest.Get(i - 1);
        }
    }

become a super magical list creator