List(CS61B学习记录)

news2024/9/22 23:23:13
  • 问题引入
    在这里插入图片描述

上图中,赋给b海象的weight会改变a海象的weight,但x的赋值又不会改变y的赋值

Bits

要解释上图的问题,我们应该从Java的底层入手
在这里插入图片描述
相同的二进制编码,却因为数据类型不同,输出不同的值

变量的声明

基本类型

Java does not write anything into the reserved box when a variable is declared. In other words, there are no default values. As a result, the Java compiler prevents you from using a variable until after the box has been filled with bits using the = operator. For this reason, I have avoided showing any bits in the boxes in the figure above.在这里插入图片描述
在这里插入图片描述

引用类型

When we declare a variable of any reference type (Walrus, Dog, Planet, array, etc.), Java allocates a box of 64 bits, no matter what type of object.
在这里插入图片描述
96位大于64位,这似乎有些矛盾:
在Java中:the 64 bit box contains not the data about the walrus, but instead the address of the Walrus in memory.

Gloden rule

在这里插入图片描述

在这里插入图片描述

练习

在这里插入图片描述
答案:B
解析:

  • 在调用方法(函数)时,doStuff方法中的int x与main方法中的int x 实际上处于两个不同的scope(调用方法时会new 一个scope,并将main方法中的x变量的位都传递给doStuff方法中的x变量,即值传递),所以x = x - 5实际上只作用于doStuff方法中的x,而不是main方法中的x。
  • 但对于引用类型来说,引用类型储存的是引用的地址,所以在进行值传递时传递的是对象的地址,所以doStuff方法中的int x与main方法中的walrus实际上指向相同的一个对象,这使得doStuff中执行的语句会作用于main方法中walrus指向的对象,所以反作用于main方法中的walrus

IntLists

在这里插入图片描述
在这里插入图片描述

使用递归求链表中元素的个数

/** Return the size of the list using... recursion! */
public int size() {
    if (rest == null) {
        return 1;
    }
    return 1 + this.rest.size();
}

Exercise: You might wonder why we don’t do something like if (this == null) return 0;. Why wouldn’t this work?

Answer: Think about what happens when you call size. You are calling it on an object, for example L.size(). If L were null, then you would get a NullPointer error!

不使用递归求元素个数

/** Return the size of the list using no recursion! */
public int iterativeSize() {
    IntList p = this;
    int totalSize = 0;
    while (p != null) {
        totalSize += 1;
        p = p.rest;
    }
    return totalSize;
}

求第n个元素

在这里插入图片描述

SLList

接下来我们对IntList进行改进,使链表看上去不那么naked
在这里插入图片描述
有了节点类,现在我们补充上链表类

public class SLList {
    public IntNode first;

    public SLList(int x) {
        first = new IntNode(x, null);
    }
}

对比SLList和IntList,我们发现,在使用SLList时无需强调null

IntList L1 = new IntList(5, null);
SLList L2  = new SLList(5);

addFirst() and getFirst()

  /** Adds an item to the front of the list. */
    public void addFirst(int x) {
        first = new IntNode(x, first);
    }
    /** Retrieves the front item from the list. */
    public int getFirst() {
        return first.item;
}

private and nested class

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);
    }
    /** Adds an item to the front of the list. */
    public void addFirst(int x) {
        first = new IntNode(x,first);
    }
    /** Retrieves the front item from the list. */
    public int getFirst() {
        return first.item;
    }
}

addLast() and Size()

/** Adds an item to the end of the list. */
public void addLast(int x) {
    IntNode p = first;

    /* Advance p to the end of the list. */
    while (p.next != null) {
        p = p.next;
    }
    p.next = new IntNode(x, null);
}
/** Returns the size of the list starting at IntNode p. */
private static int size(IntNode p) {
    if (p.next == null) {
        return 1;
    }

    return 1 + size(p.next);
}

优化低效的Size()方法

在改变链表的Size时直接记录下,就可以不需要在Size()方法中遍历链表了

public class SLList {
    ... /* IntNode declaration omitted. */
    private IntNode first;
    private int size;

    public SLList(int x) {
        first = new IntNode(x, null);
        size = 1;
    }

    public void addFirst(int x) {
        first = new IntNode(x, first);
        size += 1;
    }

    public int size() {
        return size;
    }
    ...
}

空链表( The Empty List)

创建空链表很简单(对于SLList),但会导致addLast()方法报错

public SLList() {
    first = null;
    size = 0;
}
public void addLast(int x) {
    size += 1;
    IntNode p = first;
    while (p.next != null) {
        p = p.next;
    }

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

p指向null,故p.next会出现空指针异常

addLast()改进(使用分支)
public void addLast(int x) {
    size += 1;

    if (first == null) {
        first = new IntNode(x, null);
        return;
    }

    IntNode p = first;
    while (p.next != null) {
        p = p.next;
    }

    p.next = new IntNode(x, null);
}
addLast()改进(更robust的做法)

对于存在很多特殊情况需要讨论的数据结构,上面的方法就显得十分低效。
故我们需要考虑将其改进为一个具有普适性的方法:将first改为sentinal.next
sentinal将会永远存在于链表的上位

package lists.sslist;

public class SLList {
    public class IntNode {
        public int item;
        public IntNode next;
        public IntNode(int i, IntNode n) {
            item = i;
            next = n;
        }
    }

    private IntNode sentinel;
    private int size;

    public SLList() {
        sentinel = new IntNode(63,null);
        size = 0;
    }

    public SLList(int x) {
        sentinel = new IntNode(63,null);
        sentinel.next = new IntNode(x, null);
        size = 1;
    }

    /** Adds an item to the front of the list. */
    public void addFirst(int x) {
        sentinel.next = new IntNode(x, sentinel.next);
        size += 1;
    }

    /** Retrieves the front item from the list. */
    public int getFirst() {
        return sentinel.next.item;
    }

    /** Returns the number of items in the list. */
    public int size() {
        return size;
    }

    /** Adds an item to the end of the list. */
    public void addLast(int x) {
        IntNode p = sentinel;

        /* Advance p to the end of the list. */
        while (p.next != null) {
            p = p.next;
        }
        p.next = new IntNode(x, null);
    }

    /** Crashes when you call addLast on an empty SLList. Fix it. */
    public static void main(String[] args) {
        SLList x = new SLList();
        x.addLast(5);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1518506.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【数据结构和算法初阶(C语言)】栈的概念和实现(后进先出---后来者居上的神奇线性结构带来的惊喜体验)

目录 1.栈 1.1栈的概念及结构 2.栈的实现 3.栈结构对数据的处理方式 3.1对栈进行初始化 3.2 从栈顶添加元素 3.3 打印栈元素 3.4移除栈顶元素 3.5获取栈顶元素 3.6获取栈中的有效个数 3.7 判断链表是否为空 3.9 销毁栈空间 4.结语及整个源码 1.栈 1.1栈的概念及结构 栈&am…

电力运维物联网平台

我们构建软硬件结合的生态系统,打造电力设备9物联平台,实现软件、硬件、平台、云数据一体化。 在硬件领域通过自主研发推出了一系列电力领域的硬件设备,包括:智能电力测控终端、智能电力采集终端等产品。在软件领域,我们搭…

韩国大带宽服务器的数据中心位置

很多用户会选择韩国大宽带服务器,那么韩国大带宽服务器的数据中心位置在哪,rak小编为您整理发布韩国大带宽服务器的数据中心位置。 韩国大带宽服务器的数据中心通常位于**首尔及其周边地区**。 韩国因其地理位置的优势,拥有丰富的网络带宽资源…

【智能算法】白鲨算法(AVOA)原理及实现

目录 1.背景2.算法原理2.1算法思想2.2算法过程 3.代码实现4.参考文献 1.背景 2022年,Braik 等人受到白鲨捕食行为启发,提出了非洲秃鹫优化算法(White Shark Optimizer, WSO)。 2.算法原理 2.1算法思想 海洋中白鲨拥有敏锐的感知、听觉和嗅觉&#xf…

【竞技宝】LOL:sheer对位压制369 JDG鏖战三局力克TES

北京时间2024年3月15日,英雄联盟LPL2024春季常规赛继续进行,昨日共进行三场比赛,第三场比赛由TES对阵JDG。本场比赛前两局双方战至1-1平,决胜局JDG前期就打出完美节奏,中期两次团灭TES后轻松取胜,最终JDG鏖战三局击败TES。以下是本场比赛的详细战报。 第一局: TES:鳄鱼、盲僧、…

九千元家用投影仪怎么样:当贝X5 Ultra万元内天花板配置

投影仪市场正在最贱扩大,越来越的投影品牌纷纷加入市场,一方面可以促成市场的发展,但是宁一方面,市场的乱象也在不断扩大。对于数码新手来说选择一款适合的投影仪变成了一件难事,太多的品牌和产品不知道该如何选择&…

L1-5 猜帽子游戏

宝宝们在一起玩一个猜帽子游戏。每人头上被扣了一顶帽子,有的是黑色的,有的是黄色的。每个人可以看到别人头上的帽子,但是看不到自己的。游戏开始后,每个人可以猜自己头上的帽子是什么颜色,或者可以弃权不猜。如果没有…

非常有用的Python 20个单行代码

有用的 Python 单行代码片段,只需一行代码即可解决特定编码问题! 在本文中,云朵君将分享20 个 Python 一行代码,你可以在 30 秒或更短的时间内轻松学习它们。这种单行代码将节省你的时间,并使你的代码看起来更干净且易…

数码管动态扫描显示

摸鱼记录 Day_16 (゚O゚) review 前边已经学习了: 串口接收:Vivado 串口接收优化-CSDN博客 1. 今日摸鱼任务 串口接收数据 并用数码管显示 (゚O゚) 小梅哥视频: 17A 数码管段码显示与动态扫…

06. Redis架构-哨兵

简介 什么是哨兵 Redis的主从模式下,主节点一旦发生故障便不能提供服务,需要人工干预。手动将从节点晋升为主节点,同时还需要修改客户端配置。 Sentinel(哨兵)架构解决了Redis主从人工干预的问题。 Redis Sentinel是…

《鸟哥的Linux私房菜》第6章——总结与习题参考答案

目录 一、 简介 二、一些新了解的指令 1.touch- 修改文件时间或创建新文件 2.umask-新建文件/目录的默认权限 3.文件隐藏属性 4.文件特殊权限 5.file-观察文件类型 三、简答题部分 一、 简介 本章介绍了一些常用的文件与目录指令,包括新建/删除/复制/移动/查…

Spring Cloud Alibaba微服务从入门到进阶(一)(SpringBoot三板斧、SpringBoot Actuator)

Springboot三板斧 1、加依赖 2、写注解 3、写配置 Spring Boot Actuator Spring Boot Actuator 是 Spring Boot 提供的一系列用于监控和管理应用程序的工具和服务。 SpringBoot导航端点 其中localhost:8080/actuator/health是健康检查端点,加上以下配置&#xf…

卖木头块(Lc2312)——动态规划

给你两个整数 m 和 n ,分别表示一块矩形木块的高和宽。同时给你一个二维整数数组 prices ,其中 prices[i] [hi, wi, pricei] 表示你可以以 pricei 元的价格卖一块高为 hi 宽为 wi 的矩形木块。 每一次操作中,你必须按下述方式之一执行切割操…

在SwiftUI中使用Buider模式创建复杂组件

在SwiftUI中使用Buider模式创建复杂组件 我们在前面的博客闲聊SwiftUI中的自定义组件中聊到了如何在SwiftU中创建自定义组件。 在那里,我们创建了一个非常简单的组件RedBox,它将展示内容增加一个红色的边框。 RedBox非常简单,我们用普通的方…

电梯机房秀

每天乘坐电梯,您见过电梯的机房吗?来,跟着小伍去看看吧。Lets go! 电梯还能节能呢,您知道么?正好,小伍一块带您看看电梯节能装置(●◡●) 目前电梯节能装置已广泛应用于三菱、富士、日立、奥的斯…

电梯机房秀 系列二

上次小伍带大家看了部分机房的照片,并且简单介绍了一下电梯能量回馈装置,小伙伴们表示很新奇,没看够,今天小伍又来了,带大家看一下电梯能量回馈装置到底安装在电梯什么位置。跟着小伍去看看吧。Lets go! 电…

【MySQL基础】MySQL基础操作

文章目录 🍉什么是数据库?🍓MySQL数据库🧀1.数据库操作🍆1.1展示数据库🍆1.2创建数据库🍆1.3使用数据库🍆1.4删除数据库 🧀2.常用数据类型🧀3.数据表操作&…

电视盒子解析安装包失败,安卓4.4安装不了kodi的解决方法,如何安装kodi

有些安卓电视或者电视盒子的安卓系统版本太低、自身架构或者屏蔽了安装其他应用的功能,下载的Kodi apk安装包提示无法安装,解析程序包时出现问题、解析出错无法安装、[INSTALL_FAILED_OLDER_SDK]、此应用与您的电视不兼容。 解决方法: 1、3…

分享一下自己总结的7万多字java面试笔记和一些面试视频,简历啥的,已大厂上岸

分享一下自己总结的7万多字java面试笔记和一些面试视频,简历啥的,已大厂上岸 总结的面试资料:面试资料 SSM SSM搭建的版本有很多,例如有一个版本可以这么搭建,两个核心配置文件web.xml,applicationContext.xml。1.前…

电影票预约系统---c++实现

使用 1.打开mysql对应的数据库-->prodb 打开数据库:mysql -uroot -p 查看数据库:show databases; 使用数据库:use prodb; 查看用户信息:select * from user_info 2.打开sever 3.打开client 编译命令 server.cpp命令 g -…