2019Spring-CS61B学习笔记-1&2&3

01 课堂笔记
The continue statement skips the current iteration of the loop, effectively jumping straight to the increment condition.
By contrast, the break keyword completely terminates the innermost loop when it is called.
for(String s: a){}


02 课堂笔记
Compilation :

  • Hello.java–>javac(Compiler)–>Hello.class–>java(Interpreter)–>stuff happens
  • why make a class file?
    1) .class file has been type checked. Distributed code is safer.
    2) .class file are ‘simpler’ for machine to execute. Distributed code is faster.
    3) Minor benefit: Protects your intellectual property. No need to give out source.

Defining and Instantiating Classes:

  • Every method(function) is associated with some class. To run a class, we must define a main method. And not all classes have a main method!
  • Object Instantiation:
    1) Classes can contain not just functions(methods), but also data.
    2) Class can be instantiated as objects.
    • Create a single class, and then create instances of this class.
    • The class provides a blueprint that all instances will follow.
    • Defining a Typical Class(Terminology)
      1) Instance Variable实例变量: Can have as many of these as you want.
      2) Constructor构造函数: Determines how to instantiate the classes.
      3) None static method非静态函数(Instance method): If the method is going to be invoked by an instance of the class (as in the next slide), then it should be non-static. (invocations调用 – invoke调用)

Arrays:

  • To create an array of objects:
    1) First use the new keyword to create the array.Dog[] dogs = new Dog[2];
    2) Then use new again for each object that u want to put in the array.Dogs[0] = new Dog(8);

Static vs. Non-static:

  • Key differences for methods:
    1) Static methods are invoked using the class name, e.g. Dog.makeNoise();
    2) Instance methods are invoked using an instance name, e.g. maya = new Dog(51); maya.makeNoise();
    3) Static method can not access “my” instance variables, bcz there is no “me”.

  • Why static methods:
    1) Some classes are never instantiated.(抽象类)–>Math x = Math.round(5.6);
    2) Static methods are invoked using the class name, e.g. public static Dog maxDog(Dog d1, Dog d2){} --> Dog bigger = Dog.maxDog(d1, d2);不是创建函数的时候using the class name,而是实现函数的时候调用Dog.maxDog

  • 我为什么总忘记return的时候要把void类改成return对应的类

  • 设置了method为static就不能调用this -->static method can not access “my” instance variables.

  • static variable,每个instance对应的variable都是一样的值。

  • Important:
    1) A variable or method defined in a class is also called a member of that class.
    2) Static members are accessed using class name, e.g. Dog.maxDog.静态的variable和method需要class name才能调用
    3) Non-static members cannot be invoked using class name.非静态的method和variable不能用class name来调用,必须specific到instance
    4) Static methods must access instance variables via a specific name.静态method要通过specific instance来调用实例变量 = instance variables = non-static variables.public static Dog maxDog(Dog d1, Dog d2){} --> Dog bigger = Dog.maxDog(d1, d2);


02 READING笔记
Nested(嵌套) Classes :

  • embed a class declaration inside of another for just this situation: At the moment, we have two .java files: IntNode and SLList. However, the IntNode is really just a supporting character in the story of SLList.
public class SLList {
       public class IntNode {
            public int item;
            public IntNode next;
            public IntNode(int i, IntNode n) {
                item = i;
                next = n;
            }
       }

       private IntNode first; 

       public SLList(int x) {
           first = new IntNode(x, null);
       } 
...
  • why nested classes?
    1) Nested Classes are useful when a class doesn’t stand on its own and is obvious subordinate to another class.
    2) Make the nested class private if other classes should never use the nested class.
    3) BTW, we can declare the nested class static, if it never uses any of the enclosing class’s instance variables or methods.This saves a bit of memory, because each nested class no longer needs to keep track of how to access its enclosing class.

Caching :

  • Putting aside data to speed up retrieval.
  • e.g.把size++加入到每一个小环节内,以抵消循环的浪费。

Generic List :

  • 在定义的时候是(之前文章中有提及过,这个Name跟T一样,就相当于是一个占位符而已)
public class DLList<Name>(){}
public void addFirst(Name x,...){}
  • 在使用的时候是
public static void main(String[] args){ 
	DLList<String> s1 = new DLList<String>('xxx');
	s1.addFirst(String x, ...)
}
  • 要注意的是在declare或者instantiate的时候,应该使用reference type!If you need to instantiate a generic over a primitive type, use Integer, Double, Character, Boolean, Long, Short, Byte, or Float instead of their primitive equivalents.

Array :

  • Array creation:
x = new int[3];
y = new int[]{1, 2, 3, 4, 5};
int[] z = {9, 10, 11, 12, 13};
  • Array copy: System.arraycopy takes five parameters:
The array to use as a source
Where to start in the source array
The array to use as a destination
Where to start in the destination array
How many items to copy

System.arraycopy(b, 0,x, 3, 2) is the equivalent of x[3:5] = b[0:2] in Python.
但是array1 = array2和使用System.arraycopy(array1,…,array2,…,…)运行过程是不一样的!!!

public class ArrayDanger {
	    public static void main(String[] args) {
	        int[][] x = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

	        int[][] z = new int[3][];
	        z[0] = x[0];
	        z[1] = x[1];
	        z[2] = x[2];
	        z[0][0] = -z[0][0];
	
	        int[][] w = new int[3][3];
	        System.arraycopy(x[0], 0, w[0], 0, 3);
	        System.arraycopy(x[1], 0, w[1], 0, 3);
	        System.arraycopy(x[2], 0, w[2], 0, 3);
	        w[0][0] = -w[0][0];
	        
	        System.out.println("x[0][0]=" + x[0][0] + ", w[0][0]=" + w[0][0]);
	    }
} 
Program Output: x[0][0]=-1, w[0][0]=1
  • Resizing Arrays: create a new array, and then assign its value to the original array. It’s a bit of a misnomer since the array doesn’t actually change size, we are just making a new one that has a bigger size.
int a = new int[size + 100];
System.arraycopy(items, 0, a, 0, size);
a[size] = x;
items = a;
size = size + 1;
public void addLast(int x) {
      if(size == items.length) {
           int[] a = new int[size + 100];
           System.arraycopy(items, 0, a, 0, size);
           items = a;
      }
      size = size + 1;
      items[size] = x;
}
  • SSList和AList的AddLast功能效率对比:SSList呈线性形式增长,而AList呈parabola(抛物线)形式增长。

  • 改良方案:将线性增长+的resize过程改为非线性增长*,即resize的大小由size+100改为size*2

  • Geometric resizing:
    定义“usage ratio”内存利用率为R = size / items.length
    典型的resize方案是Hald array size when R < 0.25;

后续会更多解释Tradeoffs between time and space efficiency for a variety of algorithms and data structures

  • Generic ALists:
    由于Generic Array在Java中是不存在的,因此Glorp[] items = new Glorp[8];是不被允许的;
    在使用过程中应当使用的语句为:Glorp[] items = (Glorp[]) new Object[8];
  • 最后一点就是we null out any items that we “delete”.对于泛型类型,我们最好清空对存储对象的引用,这是为了避免内存的"loitering"(闲逛)。java只在最后一个引用丢失的时候destroy objects,因此如果我们不null out the reference,Java就不会把对应的object扔进garbage。

03 Testing and Selection Sort

  • Test before 实现类

JUnit Testing :

  • The code is much simpler than the AD Hoc Testing
  • @Test这个annotation要加在TestMethod前一行
  • assertEquals是常用的Junit tests,检测a variable’s actual value is equivalent to its excepted value.使用assertEqualsassertTrue时All tests must be non-static
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值