包含标签 CS61B 的文章

【CS61B】LEC9 Extends, Casting, Higher Order Functions

LEC9 Extends, Casting, Higher Order Functions 1 接口和实现 Interface and Implement 早先我们介绍了类和接口,我们意识到在编写类时,有时会编写很多冗余代码。 这将我们引向继承,即某个对象不需要重新定义其父对象的所有性质。我们可以从接口和类继承,语法略有不同。 对于要继承接口性质的类,语法如下(其中SLList是一个类,而List61B是一……

阅读全文

【CS61B】LEC8 Inheritance, Implements

LEC8 Inheritance, Implements 1 方法重载 Overloading: 在Java中,类方法名字可以相同,但参数不同。例如,Math 类可以具有 add(int a, int b) 方法和 add(float a, float b) 方法。 Java编译器足够智能,可以根据传入的参数选择正确的方法。具有相同名称但不同参数的方法被认为被重载。 2 使代码通用: 考虑一个仅使用 AList 作为参数的 largestNumber 方法。缺点是,无论我们……

阅读全文

【CS61B】LEC6 ALists, Resizing, vs. SLists

ALists, Resizing, vs. SLists https://sp18.datastructur.es/ 列表与数组 我们的 DLList 有一个缺点。获得第i个项目很慢;我们必须从头到尾扫描列表中的每个项目,直到到达第 i 个项目为止。但是,对于名为 A 的数组,我们可以使用方括号表示法 A[i] 快速访问第i个项目。因此,我们的目标是实现带有数组的列表。 AList AList 将具有与 DLList 相同的API,这意味着它将具有与 DLList 相……

阅读全文

【CS61B】LEC5 DLLists, Arrays

DLLists, Arrays https://sp18.datastructur.es/ SLList的缺点 addLast() 方法非常慢!我们无法添加到列表的中间。另外,如果我们的列表很大,那么我们必须从最前面开始,并一直循环到列表的最后,然后再添加元素。 一个Naive的解决方法 回想一下,我们将列表的大小作为SLList的实例变量进行了缓存。如果我们也缓存列表中的最后一个元素怎……

阅读全文

【CS61B】LEC4 SLLists, Nested Classes, Sentinel Nodes

SLLists, Nested Classes, Sentinel Nodes https://sp18.datastructur.es/ 裸数据结构Naked Data Structure IntLists很难使用。为了正确使用 IntLists,即使对于简单的列表相关任务,程序员也必须理解并利用递归。 Adding Clothes 我们首先将 IntList 类转变为 IntNode 类。然后我们将删除IntNode类中的所有方法,接下来我们将创建一个名为 SLList 的新类,该类首先包含实例变量,并……

阅读全文

【CS61B】LEC3 References, Recursion, and Lists

References, Recursion, and Lists https://sp18.datastructur.es/ Bits: 计算机将信息存储为内存,并使用0或1的位序列表示此信息。 Primitives: Primitives是信息的表示。 Java有8种原始类型:byte, short, int, long, float, double, boolean, and char。每个基本类型由一定数量的位表示。例如,整型是32位,而字节是8位。 **声明基元:**当我们将变量声……

阅读全文

【CS61B】LEC2 Defining and Using Classes

1 Static vs Non-Static Methods 1.1 Static Methods Java中的所有代码都必须是类class中的一部分,大多数代码在方法methods中: public class Dog { public static void makeNoise() { System.out.println("Bark!"); } } 如果运行 Dog 类,将会报错: $ java Dog Error: Main method not found in class Dog, please define the main method as: public static void main(String[] args) 为了执行Dog类,我们需要添加一个main方法。或者,我们也可以单独创建一个 DogLauncher 类来执行……

阅读全文

【CS61B】LEC1 Intro, Hello World

从今天开始,将follow数据结构的一门经典课程,UC Berkeley的CS61B。 1 Hello World Let’s look at our first Java program. When run, the program below prints “Hello world!” to the screen. public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } } For those of you coming from a language like Python, this probably seems needlessly verbose. However, it’s all for good reason, which we’ll come to understand over the next couple of weeks. Some key syntactic features to notice: The program consists of a class declaration, which is declared using the keywords public class. In Java, all code lives inside of classes. The code……

阅读全文