当前位置: 首页 > 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…...

[2025CVPR]DeepVideo-R1:基于难度感知回归GRPO的视频强化微调框架详解

突破视频大语言模型推理瓶颈,在多个视频基准上实现SOTA性能 一、核心问题与创新亮点 1.1 GRPO在视频任务中的两大挑战 ​安全措施依赖问题​ GRPO使用min和clip函数限制策略更新幅度,导致: 梯度抑制:当新旧策略差异过大时梯度消失收敛困难:策略无法充分优化# 传统GRPO的梯…...

【JavaEE】-- HTTP

1. HTTP是什么&#xff1f; HTTP&#xff08;全称为"超文本传输协议"&#xff09;是一种应用非常广泛的应用层协议&#xff0c;HTTP是基于TCP协议的一种应用层协议。 应用层协议&#xff1a;是计算机网络协议栈中最高层的协议&#xff0c;它定义了运行在不同主机上…...

k8s从入门到放弃之Ingress七层负载

k8s从入门到放弃之Ingress七层负载 在Kubernetes&#xff08;简称K8s&#xff09;中&#xff0c;Ingress是一个API对象&#xff0c;它允许你定义如何从集群外部访问集群内部的服务。Ingress可以提供负载均衡、SSL终结和基于名称的虚拟主机等功能。通过Ingress&#xff0c;你可…...

关于iview组件中使用 table , 绑定序号分页后序号从1开始的解决方案

问题描述&#xff1a;iview使用table 中type: "index",分页之后 &#xff0c;索引还是从1开始&#xff0c;试过绑定后台返回数据的id, 这种方法可行&#xff0c;就是后台返回数据的每个页面id都不完全是按照从1开始的升序&#xff0c;因此百度了下&#xff0c;找到了…...

三体问题详解

从物理学角度&#xff0c;三体问题之所以不稳定&#xff0c;是因为三个天体在万有引力作用下相互作用&#xff0c;形成一个非线性耦合系统。我们可以从牛顿经典力学出发&#xff0c;列出具体的运动方程&#xff0c;并说明为何这个系统本质上是混沌的&#xff0c;无法得到一般解…...

Spring是如何解决Bean的循环依赖:三级缓存机制

1、什么是 Bean 的循环依赖 在 Spring框架中,Bean 的循环依赖是指多个 Bean 之间‌互相持有对方引用‌,形成闭环依赖关系的现象。 多个 Bean 的依赖关系构成环形链路,例如: 双向依赖:Bean A 依赖 Bean B,同时 Bean B 也依赖 Bean A(A↔B)。链条循环: Bean A → Bean…...

使用Spring AI和MCP协议构建图片搜索服务

目录 使用Spring AI和MCP协议构建图片搜索服务 引言 技术栈概览 项目架构设计 架构图 服务端开发 1. 创建Spring Boot项目 2. 实现图片搜索工具 3. 配置传输模式 Stdio模式&#xff08;本地调用&#xff09; SSE模式&#xff08;远程调用&#xff09; 4. 注册工具提…...

uniapp 实现腾讯云IM群文件上传下载功能

UniApp 集成腾讯云IM实现群文件上传下载功能全攻略 一、功能背景与技术选型 在团队协作场景中&#xff0c;群文件共享是核心需求之一。本文将介绍如何基于腾讯云IMCOS&#xff0c;在uniapp中实现&#xff1a; 群内文件上传/下载文件元数据管理下载进度追踪跨平台文件预览 二…...

AxureRP-Pro-Beta-Setup_114413.exe (6.0.0.2887)

Name&#xff1a;3ddown Serial&#xff1a;FiCGEezgdGoYILo8U/2MFyCWj0jZoJc/sziRRj2/ENvtEq7w1RH97k5MWctqVHA 注册用户名&#xff1a;Axure 序列号&#xff1a;8t3Yk/zu4cX601/seX6wBZgYRVj/lkC2PICCdO4sFKCCLx8mcCnccoylVb40lP...

Java数组Arrays操作全攻略

Arrays类的概述 Java中的Arrays类位于java.util包中&#xff0c;提供了一系列静态方法用于操作数组&#xff08;如排序、搜索、填充、比较等&#xff09;。这些方法适用于基本类型数组和对象数组。 常用成员方法及代码示例 排序&#xff08;sort&#xff09; 对数组进行升序…...