Thumbnail: java

CS61B_1-5

by on under Java
8 minute read

Static and Non-static

public class Demo {
    public class Dog {
        public static void makeNoise() {
            System.out.println("Bark!");
        }

        public void makeNoise() {
            System.out.println("Bark!");
        }
    }

    public static void main(String[] args) {

        /** Calling static method */
        Dog.makeNoise();

        /** Calling Non-static method */
        Dog MyDog = new Dog();
        MyDog.makeNoise();
    }
}

Key difference between static and non static (instance) methods:

  • static method are invoked using the class name,
    e.g. Dog.makeNoise();
  • Instance methods are invoked using an instance name,
    e.g. MyDog.makeNoise();
  • Static methods can’t access “my” instance variables, because there is no “me”.

Selection sort

public class SelectionSort {

    public static void sort(String[] X) {
        
        int start = 0;

        while (start < X.length) {

            int SmallestIndex = FindTheSmallest(X, start);
            
            swap(X, start, SmallestIndex);
            
            start += 1;
        }
    }

    /** return the smallest String in X , starting at start*/
    private String findSmallest(String[] X, int k) {

        int SmallestIndex = start;

        for (int i = start; i < X.length; i += 1) {

            int cmp = X[i].compareTo(X[SmallestIndex]);

            if (cmp < 0) {
                SmallestIndex = i;
            }
        }
        return SmallestIndex;
    }
     
    /** swap item a and b */
    private void swap(String[] input, int a, int b) {
        
        String Tmp = X[a];

        X[a] = X[b];
        
        X[b] = Tmp;
    }

} 

Selction sorting a list of N items:

  • Find the smallest item.
  • Move it to the front
  • Selection sort the remaining N - 1 items (without touching front item!).

DataType

众所周知 java 一共有 9 种数据类型,

1 byte = 8 bits
boolean = 1 bit    Stores true or false values
byte = 1 byte     Stores whole numbers from -128 to 127
short = 2 bytes     Stores whole numbers from -32,768 to 32,767
int = 4 bytes     Stores whole numbers from -2,147,483,648 to 2,147,483,647
long = 8 bytes     Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float = 4 bytes     Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double = 8 bytes     Stores fractional numbers. Sufficient for storing 15 decimal digits
char = 2 bytes     Stores a single character/letter or ASCII values
Reference = 8 bytes     Stores the address of the object and array

比较意料之外情理之中的是在 java 中你可以对数组的 reference 做一些类似于 object reference 的操作,比如创建一个数组 (reference),然后再给它动态绑定对象。

int[] arr;
arr = new int[] {1, 2, 3, 4};
arr = new int[] {5, 6, 7, 8};

一大堆散乱的代码,感觉网课缺了那么一点点内容导致代码有些散乱(也可能是我听的时候走神了。。)

/** ListNode */
public class ListNode<Type> {
    public Type item;
    public ListNode next;

    public ListNode(Type i, ListNode n) {
        item = i;
        next = n;
    }
}

/** SLList */
public class SLList<Type> {
    private ListNode head;
    private int size;

    /**
     * create 2 ListNode, one is the head node,
     * one is the next node of the head node
     */
    public SLList(Type x) {
        head = new ListNode(0, new ListNode(x, null));
        size = 1;
    }

    /** Adds x to the front of the list. */
    public void addFirst(Type x) {
        size += 1;
        head.next = new ListNode(x, head.next);
    }

    /** Returns the first item in the list. */
    public Type getFirst() {
        return (Type) head.next.item;
    }

    public void addLast(int x) {
        size += 1;
        ListNode p = head;

        /** Move p until it reach the end of the list */
        while (p.next != null) {
            p = p.next;
        }

        p.next = new ListNode(x, null);
    }

    /** Return the size of the list */
    public int getSize() {
        return size;
    }

    /**
     * return ith item of this list
     */
    public Type get(int i) {
        if (i == 0) {
            return (Type) head.next.item;
        }
        ListNode Item = getNode(i);
        if (Item == null) {
            return null;
        }
        return (Type)Item.item;
    }

    /** Get the reference of the rest list since ith node */
    private ListNode getNode(int i) {
        if (i > getSize() || i < 0) {
            System.out.println("Getting out of list range!!!!");
            return null;
        }
        ListNode p = this.head.next;
        while (i != 0) {
            p = p.next;
            i -= 1;
        }
        return p;
    }

    /** add the item has the value of val at ith position */
    public void add(Type val, int i) {
        size += 1;
        ListNode Item = new ListNode(val, null);
        if (i == 0) {
            Item.next = head.next;
            head.next = Item;
            return;
        }
        ListNode front = getNode(i -1);
        ListNode Next = front.next;
        front.next = Item;
        Item.next = Next;
    }

    /** Delete the ith node */
    public void delete(int i) {
        if (i <= 0 || i > getSize()) {
            System.out.println("The item is not exist.");
            return;
        }
        ListNode front = getNode(i -1);
        ListNode Item = getNode(i);
        front.next = Item.next;
        size -= 1;
    }

    /** Change the value of ith item */
    public void change(Type val, int i) {
        ListNode Item = getNode(i);
        Item.item = val;
    }
}

So, 自己照着记的一些代码先是女娲补天后来发现这天补不上了就决定直接来手开天辟地,也算是实现了增删查改吧,别问我效率怎么样,问就是不知道,也别问我边缘值会不会 Crash,问就是压根没测,也别问我泛型会不会出问题,问就是只拿 int 试了,你看不看吧~


到这里大概就是 CS61B 的前五节课左右的内容?前期节奏还是比较慢的,听起来也不算太吃力,对我而言也是逐渐接受纯英文授课还没中文字幕的一个过程,明天再接着整理了。

java