首页 > 其他分享 >03-ArrayList(CustomDynamicArray)

03-ArrayList(CustomDynamicArray)

时间:2023-01-03 15:33:50浏览次数:36  
标签:03 elements return index int ArrayList CustomDynamicArray element size

数据结构是什么?

image-20230103151100206.

image-20230103151202629.

动态数组

image-20230103151344651.

image-20230103151410186.

image-20230103151434291.

Person.java

package com.rnny;

/**
 * @author 软柠柠吖(Runny)
 * @date 2023-01-03
 */
public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("Person - finalize");
    }
}

Assert.java

package com.rnny;

/**
 * @author 软柠柠吖(Runny)
 * @date 2023-01-01
 */
public class Assert {
    public static void test(boolean value) {
        try {
            // if (value) {} 与 if (!value) {}❓
            //  答:if (value):表示 value 为 true 时,执行 if 语句
            //      => 简单理解为:当 value 是真的时,执行 if 语句(额……有点多余)
            //      if (!value):表示 value 为 false 时,执行 if 语句
            //      => 简单理解为:当 value 为假的时,执行 if 语句。
            // 总结:value => true ==> if (value) {}
            //      value => false ==> if (!value) {}
            if (!value) {
                throw new Exception("测试未通过❗");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ArrayList.java

ArrayList<E>

image-20230103151726785.

package com.rnny;

/**
 * @author 软柠柠吖(Runny)
 * @date 2023-01-01
 */
@SuppressWarnings("unchecked")
public class ArrayList<E> {
}

成员变量

/**
 * 元素的数量
 */
private int size;
/**
 * 所有的元素
 */
private E[] elements;
/**
 * 默认容量 <br>
 * static:保证变量的内存空间仅此一份
 */
private static final int DEFAULT_CAPACITY = 16;
/**
 * 元素未发现
 */
private static final int ELEMENT_NOT_FOUND = -1;

构造函数

image-20230103151747948.

public ArrayList() {
    // this(参数):调用有参构造
    this(DEFAULT_CAPACITY);
}

public ArrayList(int capacity) {
    // > VS <(大于号与小于号如何区分❓)
    //  答:从左向右看(正常看的顺序),如果为大头,则为大于号 '>'
    //                              如果为小头,则为小于号 '<'

    // 等价于:capacity = Math.max(capacity, DEFAULT_CAPACITY);
    capacity = capacity < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity;
    this.size = 0;
    // 对象数组中每个位置存储的是什么❓
    //  答:地址❗对象数组中存储的是对象的地址,而不是具体的对象。
    //  如果存储的是具体的对象,那 Object 就没法用了,这样设计也不好
    //  因为具体对象的大小不确定,无法用 Object[] 接收。
    //  存储地址就能完美的解决这个问题

标签:03,elements,return,index,int,ArrayList,CustomDynamicArray,element,size
From: https://www.cnblogs.com/rnny/p/17022361.html

相关文章

  • Failed to configure a DataSource: 'url' attribute is not specified spring boot3
     springboot的配置文件名由application.yml改为bootstrap.yml或者bootstrap.properties报错:FailedtoconfigureaDataSource:'url'attributeisnotspecifiedand......
  • C语言论坛系统[2023-01-03]
    C语言论坛系统[2023-01-03]论坛系统设计课程说明需要提交的内容包括两个部分。第一部分,对代码功能的讲解。课设要求最后每个同学录制一个讲解视频,对着自己代码的功能......
  • 集合2 ArrayList
    ArrayListpublicclassList{publicstaticvoidmain(String[]args){//TODOArrayList:Array+List//List:列表,清单--按照数据插入......
  • 软件需求设计方法学全程实例剖析幻灯片01-概述[2021-03更新]
    《软件需求设计方法学全程实例剖析》幻灯片pdf文件01-09以及UMLChina模型模板、项目调查表、《软件方法(下)》目前公开内容等资料打包下载地址​​https://pan.baidu.com/s/15......
  • kafka学习笔记03消息队列的两种模式
     ①点对点模式  该种模式就是消费者会自动消费消息,消息收到之后会向消息队列进行确认收到消息,然后将该数据进行删除。 ②发布/订阅模式  可以有多个的topic,topic......
  • python的NameError: global name 'json' is not defined解决
    报错:json未定义在使用api接口的时候json.jumps调用报错解决:直接在文件头导入json包即可可以成功进行post请求......
  • Escape character is '^]'.
    通常会在服务器上试一下本机访问其他服务是否通的,这时候telnet就派上用场了例如:[work@c4-hadoop-build04~]$telnet10.132.7.516379Trying10.132.7.51...Connectedto......
  • 袋鼠云产品功能更新报告03期丨产品体验全面优化,请查收!
    年底啦~2022年即将走到尾声,不过袋鼠云对产品品质的坚持始终如一,这段时间我们对产品本身以及客户反馈的一些问题进行了持续的更新和优化,例如新增任务告警,进行了Connector相关......
  • 20230103code
    #include<bits/stdc++.h>usingnamespacestd;//gcd(a,b)=gcd(b,a%b):a,b的最大公约数=b,a%b的最大公约数intgcd(inta,intb){//returnb?gcd(b,a%b......
  • 203. 移除链表元素
    题目链接https://leetcode.cn/problems/remove-linked-list-elements/description/解题思路按照我们解决递归的思路,我们首先想,这个递归函数,应该返回什么,应该定义什么参......