当前位置: 首页 > news >正文

数据结构:ArrayList与顺序表

目录

 📖一、什么是List

📖二、线性表

📖三、顺序表

🐬1、display()方法

🐬2、add(int data)方法

🐬3、add(int pos, int data)方法

🐬4、contains(int toFind)方法

🐬5、indexOf(int toFind)方法

🐬6、get(int pos)方法

🐬7、set(int pos, int value)方法

🐬8、remove(int toRemove)方法

🐬9、size()方法

🐬10、clear()方法

🐬11、完整代码 

📖四、ArrayList简介

📖五、ArrayList的构造 

🐬1、ArrayList()无参构造

🐬2、ArrayList(int initialCapacity))带参构造

🐬3、ArrayList(Collection c)利用Collection进行构造

📖六、ArrayList使用

🐬1、addAll(Collection c)方法

🐬2、remove(int index)方法

🐬3、remove(Object o)方法

🐬4、lastIndexOf(Object o)方法

🐬5、ListsubList(int fromIndex,int toIndex)方法

📖七、ArrayList的遍历 

🐬1、for循环+下标 

🐬2、for each

🐬3、迭代器


 📖一、什么是List

在集合框架中,List是一个接口,继承自Collection。而Collection也是一个接口,同时他有一些方法,因此List种有着许多的方法。

虽然有很多方法但我们只需要记住以下一些常用方法即可 

方法解释
boolean add(E e)尾插e
void add(int index, E element)将e插入到 index 位置
boolean addAll(Collection<? extends E> c)尾插c中的元素
E remove(int index)删除 index 位置元素
boolean remove(object o)删除遇到的第一个o
E get(int index)获取下标 index 位置元素
E set(int index, E element)将下标 index 位置元素设置为 element
void clear()清空
boolean contains(Object o)判断o是否在线性表中
int indexof(object o)返回第一个o所在下标
int lastIndexof(Object o)返回最后一个o的下标
List<E> subList(int fromindex, int tolndex)截取部分 list

📖二、线性表

线性表(linearlist)是n个具有相同特性的数据元素的有限序列。线性表是⼀种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列... 线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

📖三、顺序表

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。 在数组上他们通常表现为数据的增删查改。(顺序表具有扩容能力,通常以自身的1.5倍或2倍进行扩容)

然而我们知道数组是不具备增删查改的功能的,因此我们需要自己创建一个类,自己来实现自己创建IList接口

下面我们就来实现一个顺序表

🐬1、display()方法

打印顺序表

🐬2、add(int data)方法

新增元素 , 默认在数组最后新增(尾插)

🐬3、add(int pos, int data)方法

在 pos 位置新增元素

🐬4、contains(int toFind)方法

判定是否包含某个元素

🐬5、indexOf(int toFind)方法

查找某个元素对应的位置

🐬6、get(int pos)方法

获取 pos 位置的元素

🐬7、set(int pos, int value)方法

给 pos 位置的元素设为 value

🐬8、remove(int toRemove)方法

删除第一次出现的关键字 key

🐬9、size()方法

获取顺序表长度

🐬10、clear()方法

清空顺序表

🐬11、完整代码 

Ilist接口

package List;public interface IList {void add(int data);void add(int pos, int data);boolean contains(int toFind);int indexOf(int toFind);int get(int pos);void set(int pos, int value);void remove(int toRemove);int size();void clear();void display();
}

MyArrayList类 

import List.EmptyTable;
import List.IList;
import List.Illegality;import java.nio.channels.ScatteringByteChannel;
import java.util.Arrays;public class MyArrayList implements IList {public int[] arr;public int usedsize;public static final int DEFAULT_SIZE = 10;public MyArrayList(){this.arr = new int[DEFAULT_SIZE];}@Overridepublic void display() {for (int i = 0; i < this.usedsize; i++) {System.out.print(arr[i]+" ");}System.out.println();}@Overridepublic void add(int data) {if(isFull()){Expansion();}this.arr[this.usedsize] = data;this.usedsize++;}public boolean isFull(){return this.usedsize == arr.length;}public void Expansion(){this.arr = Arrays.copyOf(this.arr, 2*this.arr.length);}@Overridepublic void add(int pos, int data) {try{check1(pos);if(isFull()){Expansion();}int right = this.usedsize;while(pos < right){arr[right] = arr[--right];}this.arr[pos] = data;this.usedsize++;}catch(Illegality e){e.printStackTrace();}}public void check1(int pos) throws Illegality{if( pos < 0 || pos > this.usedsize){throw new Illegality("pos不合法");}}@Overridepublic boolean contains(int toFind) {for (int i = 0; i < this.usedsize; i++) {if(this.arr[i] == toFind){return true;}}return false;}@Overridepublic int indexOf(int toFind) {for (int i = 0; i < this.usedsize; i++) {if(this.arr[i] == toFind){return i;}}return -1;}@Overridepublic int get(int pos) {try{empty();check2(pos);return this.arr[pos];}catch (Illegality e){e.printStackTrace();}catch (EmptyTable e){e.printStackTrace();}return -1;}public void check2(int pos) throws Illegality{if(pos < 0 || pos >= this.usedsize){throw new Illegality("pos不合法");}}public void empty() throws EmptyTable {if(this.usedsize == 0){throw new EmptyTable("顺序表为空");}}@Overridepublic void set(int pos, int value) {try{empty();check2(pos);this.arr[pos] = value;}catch (Illegality e){e.printStackTrace();}catch (EmptyTable e){e.printStackTrace();}}@Overridepublic void remove(int toRemove) {try{empty();int pos = indexOf(toRemove);if(pos == -1){return;}for (int i = pos; i < this.usedsize ; i++) {arr[pos] = arr[++pos];}this.usedsize--;}catch (EmptyTable e){e.printStackTrace();}}@Overridepublic int size() {return this.usedsize;}@Overridepublic void clear() {this.usedsize = 0;}}

异常 

package List;public class Illegality extends RuntimeException{public Illegality(String message){super(message);}
}
package List;public class EmptyTable extends RuntimeException{public EmptyTable(String message){super(message);}
}

主函数 

public class Test {public static void main(String[] args) {MyArrayList myArrayList = new MyArrayList();}
}

📖四、ArrayList简介

在上面我们自己实现了一个顺序表MyArrayList,但是在Java中编译器自带了一个顺序表ArrayList类,它同样实现了List接口,虽然它和我们自己实现的顺序表大同小异,但是它还有一些地方需要注意。

  • ArrayList是以泛型方式实现的,使用时必须要先实例化
  • ArrayList实现了RandomAccess接口,表明ArrayList⽀持随机访问
  • ArrayList实现了Cloneable接口,表明ArrayList是可以clone的
  • ArrayList实现了Serializable接口,表明ArrayList是支持序列化的
  • 和Vector不同,ArrayList不是线程安全的,在单线程下可以使用,在多线程中可以选择Vector或者 CopyOnWriteArrayList
  •  ArrayList底层是⼀段连续的空间,并且可以动态扩容,是⼀个动态类型的顺序表

📖五、ArrayList的构造 

🐬1、ArrayList()无参构造

🐬2、ArrayList(int initialCapacity))带参构造

🐬3、ArrayList(Collection<? extends E> c)利用Collection进行构造

而它传入的值是限制通配符的上界本身和他的子类 

📖六、ArrayList使用

ArrayList实现的顺序表是实现了List接口,大多的方法的使用是和我们自己实现的接口是一样的,因此下面我们会对那些不同的进行讲解

🐬1、addAll(Collection<? extends E> c)方法

尾插c中的元素

🐬2、remove(int index)方法

删除 index 位置元素

🐬3、remove(Object o)方法

删除遇到的第一个o

🐬4、lastIndexOf(Object o)方法

返回最后一个o的下标

IndexOf返回的是相同元素第一个出现的下标,而lastIndexOf返回的是最后一个的下标

🐬5、List<E>subList(int fromIndex,int toIndex)方法

截取部分 list

📖七、ArrayList的遍历 

ArrayList 可以使用三种方式遍历:for循环+下标、for each、使用迭代器

🐬1、for循环+下标 

🐬2、for each

🐬3、迭代器


好了今天的分享就到这里了,还请大家多多关注,我们下一篇见!

相关文章:

数据结构:ArrayList与顺序表

目录 &#x1f4d6;一、什么是List &#x1f4d6;二、线性表 &#x1f4d6;三、顺序表 &#x1f42c;1、display()方法 &#x1f42c;2、add(int data)方法 &#x1f42c;3、add(int pos, int data)方法 &#x1f42c;4、contains(int toFind)方法 &#x1f42c;5、inde…...

SpringBoot之核心配置

学习目标&#xff1a; 1.熟悉Spring Boot全局配置文件的使用 2.掌握Spring Boot配置文件属性值注入 3.熟悉Spring Boot自定义配置 4.掌握Profile多环境配置 5.了解随机值设置以及参数间引用 1.全局配置文件 Spring Boot使用 application.properties 或者application.yaml 的文…...

EasyExcel上传校验文件错误信息放到文件里以Base64 返回给前端

产品需求&#xff1a; 前端上传个csv 或 excel 文件&#xff0c;文件共4列&#xff0c;验证文件大小&#xff0c;类型&#xff0c;文件名长度&#xff0c;文件内容&#xff0c;如果某行某个单元格数据验证不通过&#xff0c;就把错误信息放到这行第五列&#xff0c;然后把带有…...

单片机软件定时器V4.0

单片机软件定时器V4.0 用于单片机定时执行任务等&#xff0c;比如LED GPIO等定时控制&#xff0c;内置前后台工作模式 头文件有使用例子 #ifndef __SORFTIME_APP_H #define __SORFTIME_APP_H#ifdef __cplusplus extern "C" { #endif#include <stdint.h>// #…...

超完整Docker学习记录,Docker常用命令详解

前言 关于国内拉取不到docker镜像的问题&#xff0c;可以利用Github Action将需要的镜像转存到阿里云私有仓库&#xff0c;然后再通过阿里云私有仓库去拉取就可以了。 参考项目地址&#xff1a;使用Github Action将国外的Docker镜像转存到阿里云私有仓库 一、Docker简介 Do…...

C++ 入门第26天:文件与流操作基础

往期回顾&#xff1a; C 入门第23天&#xff1a;Lambda 表达式与标准库算法入门-CSDN博客 C 入门第24天&#xff1a;C11 多线程基础-CSDN博客 C 入门第25天&#xff1a;线程池&#xff08;Thread Pool&#xff09;基础-CSDN博客 C 入门第26天&#xff1a;文件与流操作基础 前言…...

使用python将多个Excel表合并成一个表

import pandas as pd# 定义要合并的Excel文件路径和名称 file_paths [file1.xlsx, file2.xlsx, file3.xlsx, file4.xlsx, file5.xlsx]# 创建一个空的DataFrame来存储合并后的数据 merged_data pd.DataFrame()# 循环遍历每个Excel文件&#xff0c;并读取其中的数据 for file_p…...

halcon三维点云数据处理(七)find_shape_model_3d_recompute_score

目录 一、find_shape_model_3d_recompute_score例程代码二、set_object_model_3d_attrib_mod函数三、prepare_object_model_3d 函数四、create_cube_shape_model_3d函数五、获得CamPose六、project_cube_image函数七、find_shape_model_3d函数八、project_shape_model_3d函数 一…...

vue js实现时钟以及刻度效果

2025.01.08今天我学习如何用js实现时钟样式&#xff0c;效果如下&#xff1a; 一、html代码如下&#xff1a; <template><!--圆圈--><div class"notice_border"><div class"notice_position notice_name_class" v-for"item in …...

unity学习15:预制体prefab

目录 1 创建多个gameobject 2 创建prefab 2.1 创建prefab &#xff08;类&#xff09; 2.2 prefab 是一个文件 2.3 prefab可以导出 3 创建prefab variant &#xff08;子类&#xff09; 3.1 除了创建多个独立的prefab&#xff0c; 还可以创建 prefab variant 3.2 他…...

基于Thinkphp6+uniapp的陪玩陪聊软件开发方案分析

使用uni-app框架进行前端开发。uni-app是一个使用Vue.js开发所有前端应用的框架&#xff0c;支持一次编写&#xff0c;多端发布&#xff0c;包括APP、小程序、H5等。 使用Thinkphp6框架进行后端开发。Thinkphp6是一个轻量级、高性能、面向对象的PHP开发框架&#xff0c;具有易…...

MySQL - 子查询和相关子查询详解

在SQL中&#xff0c;子查询&#xff08;Subquery&#xff09;和相关子查询&#xff08;Correlated Subquery&#xff09;是非常强大且灵活的工具&#xff0c;可以用于执行复杂的数据检索和操作。它们允许我们在一个查询中嵌套另一个查询&#xff0c;从而实现更复杂的逻辑和条件…...

Android 系统签名 keytool-importkeypair

要在 Android 项目中使用系统签名并将 APK 打包时与项目一起打包&#xff0c;可以按照以下步骤操作&#xff1a; 步骤 1&#xff1a;准备系统签名文件 从 Android 系统源码中获取系统签名文件&#xff0c;通常位于 build/target/product/security 目录下&#xff0c;包括 pla…...

安卓漏洞学习(十八):Android加固基本原理

APP加固技术发展历程 APK加固整体思路 加固整体思路&#xff1a;先解压apk文件&#xff0c;取出dex文件&#xff0c;对dex文件进行加密&#xff0c;然后组合壳中的dex文件&#xff08;Android类加载机制&#xff09;&#xff0c;结合之前的apk资源&#xff08;解压apk除dex以外…...

Docker 使用Dockerfile创建镜像

创建并且生成镜像 在当前目录下创建一个名为Dockerfile文件 vi Dockerfile填入下面配置 # 使用 CentOS 作为基础镜像 FROM centos:7# 设置工作目录 WORKDIR /app# 复制项目文件到容器中 COPY bin/ /app/bin/ COPY config/ /app/config/ COPY lib/ /app/lib/ COPY plugin/ /a…...

【Python运维】利用Python实现高效的持续集成与部署(CI/CD)流程

《Python OpenCV从菜鸟到高手》带你进入图像处理与计算机视觉的大门! 解锁Python编程的无限可能:《奇妙的Python》带你漫游代码世界 持续集成与部署(CI/CD)是现代软件开发中不可或缺的实践,通过自动化测试、构建和部署流程,显著提高了开发效率与运维质量。本文详细介绍…...

成功!QT 5.15.2编译mysql驱动

首选要说明&#xff0c;5.15与6.7编译驱动是完全不同的。搞错了永远编译不出来。 参考 主要是参考安装QT&#xff0c;安装mysql等。 编译成功&#xff01;QT/6.7.2/Creator编译Windows64 MySQL驱动(MSVC版)_mingw编译qt6.7-CSDN博客 复制mysql的include和lib到一个方便的目…...

安卓NDK视觉开发——手机拍照文档边缘检测实现方法与库封装

一、项目创建 创建NDK项目有两种方式&#xff0c;一种从新创建整个项目&#xff0c;一个在创建好的项目添加NDK接口。 1.创建NDK项目 创建 一个Native C项目&#xff1a; 选择包名、API版本与算法交互的语言&#xff1a; 选择C版本&#xff1a; 创建完之后&#xff0c;可…...

第二届 Sui 游戏峰会将于 3 月 18 日在旧金山举行

3 月中旬&#xff0c;Sui 基金会和 Mysten Labs 将共同举办第二届 Sui 游戏峰会&#xff08;Sui Gaming Summit&#xff09;&#xff0c;这是一个专注于 Sui 游戏平台的 GDC 周边活动。此次峰会将与旧金山的年度游戏开发者大会&#xff08;GDC&#xff0c;Game Developers Conf…...

自动驾驶相关知识学习笔记

一、概要 因为想知道SIL、HIL是什么仿真工具&#xff0c;故而浏览了自动驾驶相关的知识。 资料来源《自动驾驶——人工智能理论与实践》胡波 林青 陈强 著&#xff1b;出版时间&#xff1a;2023年3月 二、图像的分类、分割与检测任务区别 如图所示&#xff0c;这些更高阶的…...

synchronized 学习

学习源&#xff1a; https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖&#xff0c;也要考虑性能问题&#xff08;场景&#xff09; 2.常见面试问题&#xff1a; sync出…...

【网络安全产品大调研系列】2. 体验漏洞扫描

前言 2023 年漏洞扫描服务市场规模预计为 3.06&#xff08;十亿美元&#xff09;。漏洞扫描服务市场行业预计将从 2024 年的 3.48&#xff08;十亿美元&#xff09;增长到 2032 年的 9.54&#xff08;十亿美元&#xff09;。预测期内漏洞扫描服务市场 CAGR&#xff08;增长率&…...

Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)

目录 1.TCP的连接管理机制&#xff08;1&#xff09;三次握手①握手过程②对握手过程的理解 &#xff08;2&#xff09;四次挥手&#xff08;3&#xff09;握手和挥手的触发&#xff08;4&#xff09;状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...

在Ubuntu中设置开机自动运行(sudo)指令的指南

在Ubuntu系统中&#xff0c;有时需要在系统启动时自动执行某些命令&#xff0c;特别是需要 sudo权限的指令。为了实现这一功能&#xff0c;可以使用多种方法&#xff0c;包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法&#xff0c;并提供…...

LLM基础1_语言模型如何处理文本

基于GitHub项目&#xff1a;https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken&#xff1a;OpenAI开发的专业"分词器" torch&#xff1a;Facebook开发的强力计算引擎&#xff0c;相当于超级计算器 理解词嵌入&#xff1a;给词语画"…...

PAN/FPN

import torch import torch.nn as nn import torch.nn.functional as F import mathclass LowResQueryHighResKVAttention(nn.Module):"""方案 1: 低分辨率特征 (Query) 查询高分辨率特征 (Key, Value).输出分辨率与低分辨率输入相同。"""def __…...

Selenium常用函数介绍

目录 一&#xff0c;元素定位 1.1 cssSeector 1.2 xpath 二&#xff0c;操作测试对象 三&#xff0c;窗口 3.1 案例 3.2 窗口切换 3.3 窗口大小 3.4 屏幕截图 3.5 关闭窗口 四&#xff0c;弹窗 五&#xff0c;等待 六&#xff0c;导航 七&#xff0c;文件上传 …...

android RelativeLayout布局

<?xml version"1.0" encoding"utf-8"?> <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"match_parent"android:gravity&…...

提升移动端网页调试效率:WebDebugX 与常见工具组合实践

在日常移动端开发中&#xff0c;网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时&#xff0c;开发者迫切需要一套高效、可靠且跨平台的调试方案。过去&#xff0c;我们或多或少使用过 Chrome DevTools、Remote Debug…...

【C++】纯虚函数类外可以写实现吗?

1. 答案 先说答案&#xff0c;可以。 2.代码测试 .h头文件 #include <iostream> #include <string>// 抽象基类 class AbstractBase { public:AbstractBase() default;virtual ~AbstractBase() default; // 默认析构函数public:virtual int PureVirtualFunct…...