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

【Java数据结构】初识线性表之一:顺序表

使用Java简单实现一个顺序表

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

线性表大致包含如下的一些方法:

public class MyArrayList {
    private int[] array;
    private int size;
    // 默认构造方法默认分配空间
    SeqList(){   }
    // 将顺序表的底层容量设置指定容量
    SeqList(int initcapacity){   }

     // 新增元素,默认在数组最后新增
    public void add(int data) { }
    // 在 pos 位置新增元素
    public void add(int pos, int data) { }
    // 判定是否包含某个元素
    public boolean contains(int toFind) { return true; }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) { return -1; }
    // 获取 pos 位置的元素
    public int get(int pos) { return -1; }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {   }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {   }
    // 获取顺序表长度
    public int size() { return 0; }
    // 清空顺序表
    public void clear() {   }
   
    // 打印顺序表
    public void display() {   }
}

 接下来根据上面的方法实现一个 int 类型的顺序表:

import java.util.Arrays;
public class MyArrayList {private int[] elem;private int usedSize;private static final int DEFAULT_SIZE = 10;public MyArrayList(){elem = new int[DEFAULT_SIZE];}public MyArrayList(int initCapacity){elem = new int[initCapacity];}private boolean checkCapacity(){if(this.usedSize == elem.length){return true;}return false;}public void display(){for (int i = 0; i < this.usedSize; i++) {System.out.print(this.elem[i] + " ");}}public void add(int data){if(checkCapacity()){this.elem = Arrays.copyOf(this.elem,2*elem.length);}this.elem[this.usedSize] = data;this.usedSize++;return;}public void add(int pos,int data){if(pos > this.usedSize || pos < 0){throw new PosOutOfBoundsException("插入位置错误!");}if(checkCapacity()){this.elem = Arrays.copyOf(this.elem,2*elem.length);}for (int i = this.usedSize - 1; i >=pos ; i--) {elem[i+1] = elem[i];}this.elem[pos] = data;this.usedSize++;return;}public boolean contains(int data){for (int i = 0; i < this.usedSize; i++) {if(this.elem[i] == data){return true;}}return false;}public int indexof(int data){for (int i = 0; i < this.usedSize; i++) {if(this.elem[i] == data){return i;}}return -1;}public int get(int pos){if(pos >= this.usedSize || pos < 0){throw new PosOutOfBoundsException("输入的位置错误!");}return this.elem[pos];}public void set(int pos,int data){if(pos >= this.usedSize || pos < 0){throw new PosOutOfBoundsException("输入的位置错误!");}this.elem[pos] = data;}public int size(){return this.usedSize;}public void remove(int data){if(this.contains(data)){int pos = this.indexof(data);for (int i = pos; i < this.usedSize - 1; i++) {this.elem[pos] = this.elem[pos+1];}this.usedSize--;}else{throw new PosOutOfBoundsException("没有该元素");}}public void clear(){this.usedSize = 0;return;}
}

ArrayList简介

在集合框架中,ArrayList是一个普通的类,实现了List接口,具体框架图如下:

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

ArrayList如何使用

ArrayList的构造方法

ArrayList中的构造方法:

ArrayList();//无参构造

ArrayList(Collection<? extends E> c);//利用其他 Collection 构建 ArrayList

ArrayList(int initialCapacity);//指定顺序表初始容量

 代码示例:

public class Test {public static void main(String[] args) {List<Integer> list1 = new ArrayList<>();//无参构造List<Integer> list2 = new ArrayList<>(10);//指定容量list2.add(1);list2.add(2);list2.add(3);List<Integer> list3 = new ArrayList<>(list2);//利用其他 Collection 构建 ArrayList}
}

ArrayList常见操作

尾插

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();//无参构造list.add(1);list.add(2);list.add(3);System.out.println(list);}
}

将元素插入到指定位置

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.add(1,4);System.out.println(list);}
}

尾插另一个顺序表中的元素

public class Test {public static void main(String[] args) {List<Integer>  list1 = new ArrayList<>();list1.add(4);list1.add(5);list1.add(6);List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.addAll(list1);System.out.println(list);}
}

删除指定位置元素

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.remove(1);System.out.println(list);}
}

删除指定数据

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.remove(new Integer(2));System.out.println(list);}
}

获取指定位置元素

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);System.out.println(list.get(1));}
}

将指定位置元素设置为新数据

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.set(1,4);System.out.println(list);}
}

清空顺序表

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.clear();System.out.println(list);}
}

判断一个元素是否在顺序表中

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);System.out.println(list.contains(2));System.out.println(list.contains(4));}
}

返回第一个指定元素所在下标

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.add(1);System.out.println(list.indexOf(1));}
}

返回最后一个指定元素所在下标

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.add(1);System.out.println(list.lastIndexOf(1));}
}

截取部分list

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);System.out.println(list.subList(0,2));}
}

遍历 ArrayList 的三种方法

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

public class Test {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);//使用fori遍历for (int i = 0; i < list.size(); i++) {System.out.print(list.get(i));}System.out.println();//使用foreach遍历for(Integer integer:list){System.out.print(integer);}System.out.println();//使用迭代器遍历Iterator<Integer> it = list.listIterator();while(it.hasNext()){System.out.print(it.next());}}
}

运行结果:

 ArrayList的场景使用

洗牌算法

将一副扑克牌随机打乱,并分配给三个人,每人五张牌

算法原码所在位置:

shufflecards · 一直淡水鱼/Java经典例题 - 码云 - 开源中国 (gitee.com)

杨辉三角

题目描述:

代码实现:

public class Test {public static List<List<Integer>> generate(int numRows) {List<List<Integer>> list = new LinkedList<>();for (int i = 0; i < numRows; i++) {List<Integer> row = new LinkedList<>();for (int j = 0; j < i + 1; j++) {if (j == 0 || i == j) {row.add(1);} else {row.add(list.get(i - 1).get(j - 1) + list.get(i - 1).get(j));}}list.add(row);}return list;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int numRows = scanner.nextInt();List<List<Integer>> list = generate(numRows);for (int i = 0; i < numRows; i++) {for (int j = 0; j < i + 1; j++) {System.out.print(list.get(i).get(j) + " ");}System.out.println();}}
}

 运行结果图:

相关文章:

【Java数据结构】初识线性表之一:顺序表

使用Java简单实现一个顺序表 顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构&#xff0c;一般情况下采用数组存储。在数组上完成数据的增删查改。 线性表大致包含如下的一些方法&#xff1a; public class MyArrayList { private int[] array; pri…...

对接高德开放平台API

高德开放平台API&#xff1a; https://lbs.amap.com/ 一、天气查询 天气查询: https://lbs.amap.com/api/webservice/guide/api/weatherinfo adcode城市码表下载: https://lbs.amap.com/api/webservice/download Component public class WeatherUtil {Resourceprivate GdCon…...

Linux 初识

目录 ​编辑 1.Linux发展史 1.1UNIX发展历史 1.2Linux发展历史 2.Linux的开源属性 2.1 开源软件的定义 2.2 Linux的开源许可证 2.3 开源社区与协作 3.Linux的企业应用现状 3.1 服务器 3.1.1 Web服务器 3.1.2 数据库服务器 3.1.3 文件服务器 3.1.4 电子邮件服务器 …...

CSS技巧专栏:一日一例 4.纯CSS实现两款流光溢彩的酷炫按钮特效

大家好&#xff0c;今天是 CSS技巧专栏&#xff1a;一日一例 第三篇《纯CSS实现两款流光溢彩的酷炫按钮特效》 先看图&#xff1a; 特此说明&#xff1a; 本专题专注于讲解如何使用CSS制作按钮特效。前置的准备工作和按钮的基本样式&#xff0c;都在本专栏第一篇文章中又详细…...

int类型变量表示范围的计算原理

文章目录 1. 了解2. 为什么通常情况下int类型整数的取值范围是-2147483648 ~ 21474836473. int类型究竟占几个字节4. 推荐 1. 了解 通常情况下int类型变量占4个字节&#xff0c;1个字节有8位&#xff0c;每位都有0和1两种状态&#xff0c;所以int类型变量一共可以表示 2^32 种状…...

STM32崩溃问题排查

文章目录 前言1. 问题说明2. STM32&#xff08;Cortex M4内核&#xff09;的寄存器3. 崩溃问题分析3.1 崩溃信息的来源是哪里&#xff1f;3.2 崩溃信息中的每个关键字代表的含义3.3 利用崩溃信息去查找造成崩溃的点3.4 keil5中怎么根据地址找到问题点3.5 keil5上编译时怎么输出…...

CSS 【详解】样式选择器(含ID、类、标签、通配、属性、伪类、伪元素、Content属性、子代、后代、兄弟、相邻兄弟、交集、并集等选择器)

CSS 样式选择器&#xff0c;用于选中页面中的 html 元素&#xff0c;以便添加 CSS 样式。 按渲染性能由高到低 依次是&#xff1a; ID 选择器 #id 通过元素的 id 属性选中元素&#xff0c;区分大小写 <p id"p1" >第一段</p>#p1{color: red; }但不推荐使…...

CMakeLists.txt编写思路

近期在linux编写CMakeLists.txt文件&#xff0c;整理了一些思路。 一、编写CMakeLists.txt的基本步骤和思路&#xff1a; 初始化CMake&#xff1a; 使用cmake_minimum_required指令指定CMake的最小版本要求&#xff0c;以确保兼容性。使用project指令定义项目名称和可选的语言…...

红日靶场----(三)2.漏洞利用

上期的通过一句话木马实现对目标主机的持久后门 我使用的是蚁剑&#xff0c;蚁剑安装及使用参考&#xff1a; 下载地址&#xff1a; GitHub - AntSwordProject/AntSword-Loader: AntSword 加载器 安装即使用&#xff1a; 1. 快速入门 语雀 通过YXCMS的后台GETSHELL 利用…...

LeetCode HOT100(三)滑动窗口

子数组最大平均数 I &#xff08;非hot100&#xff0c;但是滑动窗口的思想可以很好的体现&#xff0c;入门滑动窗口很好的题&#xff09; 给你一个由 n 个元素组成的整数数组 nums 和一个整数 k 。 请你找出平均数最大且 长度为 k 的连续子数组&#xff0c;并输出该最大平均数…...

数学系C++ 排序算法简述(八)

目录 排序 选择排序 O(n2) 不稳定&#xff1a;48429 归并排序 O(n log n) 稳定 插入排序 O(n2) 堆排序 O(n log n) 希尔排序 O(n log2 n) 图书馆排序 O(n log n) 冒泡排序 O(n2) 优化&#xff1a; 基数排序 O(n k) 快速排序 O(n log n)【分治】 不稳定 桶排序 O(n…...

记一下blender曲线阵列

先说一下如何正常使用这个 这一次我是用来贴瓷砖 随便创建一个mesh 然后添加一个阵列修改器&#xff0c;然后再给他添加一个curve修改器&#xff0c;使用constant offset去偏移他 这里有个小细节 我第一次创建的curve 我选取之后&#xff0c;死活无法沿着曲线阵列&#xff…...

Windows电脑安装Python结合内网穿透轻松搭建可公网访问私有网盘

文章目录 前言1.本地文件服务器搭建1.1.Python的安装和设置1.2.cpolar的安装和注册 2.本地文件服务器的发布2.1.Cpolar云端设置2.2.Cpolar本地设置 3.公网访问测试4.结语 前言 本文主要介绍如何在Windows系统电脑上使用python这样的简单程序语言&#xff0c;在自己的电脑上搭建…...

react hooks antd 父组件取子组件form表单的值

在React中&#xff0c;父组件可以使用ref来访问子组件的方法或属性。子组件包含一个表单&#xff0c; 使用forwardRef、useImperativeHandle&#xff1a;forwardRef允许组件使用ref将 DOM 节点暴露给父组件&#xff0c;使用useImperativeHandle暴露方法给父组件。 子组件&#…...

【ARMv8/v9 GIC 系列 1.7 -- GIC PPI | SPI | SGI | LPI 中断使能配置概述】

请阅读【ARM GICv3/v4 实战学习 】 文章目录 GIC 各种中断使能配置PPIs(每个处理器私有中断)SPIs(共享外设中断)SGIs(软件生成的中断)LPIs(局部中断)GIC 各种中断使能配置 在ARM GICv3和GICv4架构中,不同类型的中断(如PPIs、SPIs、SGIs和LPIs)可以通过不同的方式进…...

大数据如何推动工业数字化发展?

随着工业领域的深刻变革&#xff0c;数字化成为了驱动行业前行的核心力量。在这一转变中&#xff0c;大数据扮演着不可或缺的角色。它不仅为企业提供了洞察市场趋势、消费者行为等关键信息的窗口&#xff0c;还为企业优化生产流程、提升产品质量以及推动创新提供了强有力的支持…...

计算机网络浅谈—什么是 OSI 模型?

开放系统通信&#xff08;OSI&#xff09;模型是一个代表网络通信工作方式的概念模型。 思维导图 什么是 OSI 模型&#xff1f; 开放系统互连 (OSI) 模型是由国际标准化组织创建的概念模型&#xff0c;支持各种通信系统使用标准协议进行通信。简单而言&#xff0c;OSI 为保证…...

浪潮服务器内存物理插槽位置

浪潮服务器内存物理插槽位置 如下图所示...

windows node降级到指定版本

要在Windows上将Node.js降级到指定版本&#xff0c;你可以使用nvm&#xff08;Node Version Manager&#xff09;来管理和切换不同的Node.js版本。以下是使用nvm降级Node.js的步骤&#xff1a; 如果尚未安装nvm&#xff0c;请访问https://github.com/coreybutler/nvm-windows …...

EXSI 实用指南 2024 -编译环境 Mac OS 安装篇(一)

1. 引言 在现代虚拟化技术的快速发展中&#xff0c;VMware ESXi 作为领先的虚拟化平台&#xff0c;凭借其高性能、稳定性和丰富的功能&#xff0c;广泛应用于企业和个人用户。ESXi 能有效地提高硬件资源利用率&#xff0c;并简化 IT 基础设施的管理。然而&#xff0c;如何在 V…...

seo优化服务价格一般是多少_网站快速排名对网站访问量有什么影响

SEO优化服务价格一般是多少_网站快速排名对网站访问量有什么影响 在当前数字化经济的浪潮中&#xff0c;网站的流量和排名直接决定了企业的成功与否。SEO优化服务价格一般是多少&#xff1f;更重要的是&#xff0c;网站快速排名对网站访问量有什么影响呢&#xff1f;这两个问题…...

RoboStudio6.08学习记录(1)

一.软件安装一、下载RobotStudio软件官方1. 请登陆网址&#xff1a;https://new.abb.com/products/robotics/robotstudio。2. 单击进入页面“下载RobotStudio软件”3. 单击填写信息后&#xff0c;可以获得下载链接二、安装RobotStudio软件1. 下载完成后&#xff0c;对压缩包进行…...

Amazon Q 从入门到实战,AWS 专属 AI 助手超全指南

目录 一、Amazon Q 到底是什么 二、Amazon Q 有两个版本 1、Amazon Q Developer&#xff08;给开发者/运维&#xff09; 2、Amazon Q Bussiness&#xff08;给企业/业务人员&#xff09; 三、Amazon Q能解决什么实际问题 四、Amazon Q 和 Chat GPT 同类助手的有什么区别 …...

防晒霜真的防晒吗?揭秘SPF值背后的“光“标准

盛夏将至&#xff0c;防晒霜成为每个人的随身必备。你是否想过&#xff1a;瓶身上标注的 SPF 50、PA 是如何测出来的&#xff1f;为什么有些防晒霜涂了还是会晒黑&#xff1f;所谓的"防水防汗"真的有科学依据吗&#xff1f;这些问题的答案&#xff0c;都藏在一个精密…...

基于MATLAB的悬臂梁前3阶固有频率和振型求解(假设模态法、解析法、瑞利里兹法)

基于matlab的求解悬臂梁前3阶固有频率和振型 基于matlab的求解悬臂梁前3阶固有频率和振型,采用的方法分别是&#xff08;假设模态法&#xff0c;解析法&#xff0c;瑞利里兹法&#xff09; 程序已调通&#xff0c;可直接运行悬臂梁的振动分析总带着点工程师的浪漫——既要数学的…...

从GIS小白到地图处理高手:我的Global Mapper V26完整安装与汉化避坑实录

从GIS小白到地图处理高手&#xff1a;我的Global Mapper V26完整安装与汉化避坑实录 第一次打开Global Mapper时&#xff0c;我被满屏的英文界面和专业术语吓退了——这大概也是许多GIS初学者共同的经历。作为一款被行业专家誉为"地理信息瑞士军刀"的软件&#xff0c…...

10个HTTPie CLI高级功能实战技巧:从入门到精通API调试

10个HTTPie CLI高级功能实战技巧&#xff1a;从入门到精通API调试 【免费下载链接】cli &#x1f967; HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more. 项目地址: https:/…...

STM32标准库开发入门与实战指南

1. STM32入门指南&#xff1a;从零开始掌握标准库开发作为一名嵌入式开发者&#xff0c;我深知STM32的学习曲线有多陡峭。记得我第一次接触STM32时&#xff0c;面对密密麻麻的寄存器手册和复杂的开发环境&#xff0c;完全不知从何入手。经过多年的项目实践和教学经验&#xff0…...

OpenClaw开发环境配置:千问3.5-9B辅助的IDE插件管理

OpenClaw开发环境配置&#xff1a;千问3.5-9B辅助的IDE插件管理 1. 为什么需要AI辅助的IDE管理 作为一个长期在多个项目间切换的全栈开发者&#xff0c;我深受开发环境配置问题的困扰。每次换新电脑或者重装系统&#xff0c;光是配置VSCode插件和项目依赖就要耗费大半天时间。…...

【AI实战课程】第三章:⾃然语⾔处理的常⻅任务和⽅法

分享一个大牛的人工智能教程。零基础&#xff01;通俗易懂&#xff01;风趣幽默&#xff01;希望你也加入到人工智能的队伍中来&#xff01;请轻击人工智能教程​​​https://www.captainai.net/troubleshooter 本阶段重点讲解AI⾃然语⾔处理中的主流任务&#xff0c;如⽂本分…...