LOADING...

加载过慢请开启缓存

loading

CS61B-List

2024/4/15 Study

3-Reference/Recursion

The Golden Rule of Equals(GRoE)

在Java中,值的传递就是bits的传递

  • y=x copies all bits from x into y
    • a/b 指向同一个实例,a改变b也改变
    • a/b 作为整形复制,不相互影响

Reference

讲解引用类型的生成过程,用一些最基础的硬件方面的例子以便于理解

  • Reference Type:引用类型
  • 全零为null,非全零为指针指向地址
    • new shout the location!!!
    • a record the location

Array

  • arays也是一种object
int[] a; //一种声明
a = new int[]{0, 1, 2, 3}; //实例化

List

public class IntList{
    public int first;
    public IntList rest;

    public IntList(int f, IntList r){
        first = f;
        rest = r;
    }

    public int size(){
        if (rest == null)
            return 1;
        return 1 + this.rest.size()
        }

    public static void main(String args[]){
        IntList L = new IntList();
        L = new IntList(10, L);
        
    }
}

4-SLLists

聚焦于如何改善之前的List代码,他称之前的代码是“naked”,有点意思

#1:Rebranding

  • 换了个变量名

#2:Bureaucracy

  • 改变初始化的结构,不需要自己指定null结束节点了
IntNode X = new IntNode(10, null);

SLList Y = new SLList(10);
  • The basic SLList
    • 编写一些帮助函数,看起来更加清晰
      image.png

#4:Access Control

  • 使用关键词private
    • 你不懂就别jb乱动
    • 保护你的代码
    • 对你自己编写代码也有帮助
private IntNode first; //值无法被用.强行修改

#5:Nested Classes

  • 嵌套类

  • nested classes–>static nested classes

    • 可以节省一丢丢内存

5-DLLists

Doubly Linked Lists

  • 双向链表

Generic Lists (泛型)

如果我要创建一个字符串链表,便会出现报错,难道要再写一个字符串SLList吗?

/** <LochNess> 传入参数 类型 */
public class SLList<LochNess>{
    public LochNess item;
    public StuffNode next;
}

SLList<String> s1 = new SLList<String>("bone");

多维数组

int[][] pascalsTriangle;
pascalsTriangle = new int[4][];

pascalsTriangle = new int[4][4];