bobo老师的玩转算法系列–玩转数据结构 简单记录

文章目录

不要小瞧数组 - 制作一个数组类

1 、使用Java中的数组

数组基础

  • 把数据码成一排进行存放

  • 数组名最好要有语义 如: scores

  • 索引从零开始 0 1 2 3 4 5

  • 数组N个元素 数组最后一个索引 N - 1

简单使用

Array

public class Main {

    public static void main(String[] args) {
int[] arr = new int[20];
for (int i = 0; i < arr.length; i++)
arr[i] = i; int[] scores = new int[]{100, 99, 98};
for (int i = 0; i < scores.length; i++)
System.out.println(scores[i]); for(int score: scores)
System.out.println(score);
} }

2、二次封装属于我们自己的数组

数组基础

  • 索引可以有语意,也可以没有语意。

  • 数组最大的优点:快速查询。scores[1] 指定索引查询对应的值

  • 数组最好应用于“索引有语意”的情况。

  • 但并非所有有语意的索引都适用于数组

  • 数组也可以处理“索引没有语意的“

  • 这一章主要处理“索引没有语意的“的情况数组的使用

  • 数组创建已经固定了

制作属于我们自己的数组类

  • 索引没有语意,如何表示没有元素呢?
  • 如何添加元素?如何删除元素?
  • 基于java的数据,二次封装属于我们自己的数组类

不同的数据结构增删改查是截然不同的,甚至有些数据结构可能会忽略这四个中的某个一个动作。

数组 容量 capacity

数组中最多可以装多少个元素和实际装了多少个元素是没有关系的 ,两回事。

size 数组中的元素个数

简单定义Array类

Array类

/**
* @create 2019-11-14 21:27
*/
public class Array {
private int[] data;
private int size; //构造函数, 传入数组的容量capacity构造Array
public Array(int capacity){
data = new int[capacity];
size = 0;
} //无参数的构造函数,默认数组的容量capacity=10
public Array(){
this(10);
} //获取数组中的元素个数
public int getSize(){
return size;
} //获取数组的容量
public int getCapacity(){
return data.length;
}
//返回数组是否为空
public boolean isEmpty(){
return size == 0;
}
}

3、向数组中添加元素

向数组末添加元素e

//向所有元素后添加一个新元素
public void addLast(int e){
if (size == data.length) {
throw new IllegalArgumentException("AddLast failed.Array is full.");
}
data[size] = e;
size ++;
//data[size++] = e;
}

在特定的位置插入一个新元素e

// 在第index个位置插入一个新元素e
public void add(int index,int e){
if (size == data.length) {
throw new IllegalArgumentException("Add failed. Array is full.");
}
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
} for(int i = size -1; i >= index; i--){
data[i+1] = data[i];
}
data[index] = e;
size ++;
}

复用add

	//向所有元素后添加一个新元素
public void addLast(int e){
add(size,e);
}
//在所有元素前添加一个新元素
public void addFirst(int e){
add(0,e);
}

2-4 数组中查询元素和修改元素

toString

@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d ,capacity = %d\n", size, data.length));
res.append('[');
for (int i = 0; i < size; i ++){
res.append(data[i]);
if (i != size -1){
res.append(",");
}
}
res.append(']');
return res.toString();
}

获取index索引位置的元素

 //获取index索引位置的元素
int get(int index){
if (index < 0 || index >= size)
throw new IllegalArgumentException("Get failed.Index is illegal");
return data[index];
}

修改index索引位置的元素为e

// 修改index索引位置的元素为e
void set(int index, int e){
if (index < 0 || index >= size)
throw new IllegalArgumentException("Get failed.Index is illegal");
data[index] = e;
}

Test 测试

Main类

public class Main {

    public static void main(String[] args) {

        Array arr = new Array(20);
for (int i = 0; i < 10; i ++){
arr.addLast(i);
}
System.out.println(arr); arr.add(1,100);
System.out.println(arr); arr.addFirst(-1);
System.out.println(arr); System.out.println(arr.get(0));
arr.set(0,100);
System.out.println(arr.get(0));
} }

结果:

Array: size = 10 ,capacity = 20
[0,1,2,3,4,5,6,7,8,9]
Array: size = 11 ,capacity = 20
[0,100,1,2,3,4,5,6,7,8,9]
Array: size = 12 ,capacity = 20
[-1,0,100,1,2,3,4,5,6,7,8,9]
-1
100

5、包含,搜索和删除

删除指定index位置的元素,返回删除的元素

//从数组中删除index位置的元素,返回删除的元素
public int remove(int index){
if (index < 0 || index >= size)
throw new IllegalArgumentException("Get failed.Index is illegal");
int ret = data[index];
for (int i = index + 1; i < size; i ++ ){
data[i-1] = data[i];
}
size --;
return ret;
}

删除指定元素e

//从数组中删除元素e
public void removeElemet(int e){
int index = find(e);
if (index != -1){
remove(index);
}
}

从数组中删除第一个元素,返回删除的元素

 // 从数组中删除第一个元素,返回删除的元素
public int removeFirst(){
return remove(0);
}

从数组中删除最后一个元素,返回删除的元素

// 从数组中删除最后一个元素,返回删除的元素
public int removeLast(){
return remove(size-1);
}

查找数组中是否有元素e

//查找数组中是否有元素e
public boolean contains(int e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return true;
}
}
return false;
}

查找数组中元素e所在的索引,如果不存在元素e,则返回-1

//查找数组中元素e所在的索引,如果不存在元素e,则返回-1
public int find(int e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return i;
}
}
return -1;
}

Array 类

/**
* @author Liu Awen Email:willowawen@gmail.com
* @create 2019-11-14 21:27
*/
public class Array {
private int[] data;
private int size; //构造函数, 传入数组的容量capacity构造Array
public Array(int capacity) {
data = new int[capacity];
size = 0;
} //无参数的构造函数,默认数组的容量capacity=10
public Array() {
this(10);
} //获取数组中的元素个数
public int getSize() {
return size;
} //获取数组的容量
public int getCapacity() {
return data.length;
} //返回数组是否为空
public boolean isEmpty() {
return size == 0;
} //向所有元素后添加一个新元素
public void addLast(int e) {
add(size, e);
} //在所有元素前添加一个新元素
public void addFirst(int e) {
add(0, e);
} // 在第index个位置插入一个新元素e
public void add(int index, int e) {
if (size == data.length) {
throw new IllegalArgumentException("Add failed. Array is full.");
}
if (index < 0 || index > size) {
throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
} for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
} //获取index索引位置的元素
public int get(int index) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Get failed.Index is illegal");
return data[index];
} // 修改index索引位置的元素为e
public void set(int index, int e) {
if (index < 0 || index >= size)
throw new IllegalArgumentException("Get failed.Index is illegal");
data[index] = e;
} //查找数组中是否有元素e
public boolean contains(int e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return true;
}
}
return false;
} //查找数组中元素e所在的索引,如果不存在元素e,则返回-1
public int find(int e) {
for (int i = 0; i < size; i++) {
if (data[i] == e) {
return i;
}
}
return -1;
}
//从数组中删除index位置的元素,返回删除的元素
public int remove(int index){
if (index < 0 || index >= size)
throw new IllegalArgumentException("Get failed.Index is illegal");
int ret = data[index];
for (int i = index + 1; i < size; i ++ ){
data[i-1] = data[i];
}
size --;
return ret;
}
// 从数组中删除第一个元素,返回删除的元素
public int removeFirst(){
return remove(0);
}
// 从数组中删除最后一个元素,返回删除的元素
public int removeLast(){
return remove(size-1);
}
//从数组中删除元素e
public void removeElemet(int e){
int index = find(e);
if (index != -1){
remove(index);
}
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d ,capacity = %d\n", size, data.length));
res.append('[');
for (int i = 0; i < size; i++) {
res.append(data[i]);
if (i != size - 1) {
res.append(",");
}
}
res.append(']');
return res.toString();
}
}

Test 测试

public class Main {

    public static void main(String[] args) {

        Array arr = new Array(20);
for (int i = 0; i < 10; i ++){
arr.addLast(i);
}
System.out.println(arr); arr.add(1,100);
System.out.println(arr); arr.addFirst(-1);
System.out.println(arr);
// [-1,0,100,1,2,3,4,5,6,7,8,9]
arr.remove(2);
System.out.println(arr);
arr.removeElemet(4);
System.out.println(arr);
arr.removeFirst();
System.out.println(arr);
System.out.println("数组arr是否存在0元素" + arr.contains(0));
System.out.println("元素0的索引为" + arr.find(0));
} }

result:

Array: size = 10 ,capacity = 20
[0,1,2,3,4,5,6,7,8,9]
Array: size = 11 ,capacity = 20
[0,100,1,2,3,4,5,6,7,8,9]
Array: size = 12 ,capacity = 20
[-1,0,100,1,2,3,4,5,6,7,8,9]
Array: size = 11 ,capacity = 20
[-1,0,1,2,3,4,5,6,7,8,9]
Array: size = 10 ,capacity = 20
[-1,0,1,2,3,5,6,7,8,9]
Array: size = 9 ,capacity = 20
[0,1,2,3,5,6,7,8,9]
数组arr是否存在0元素true
元素0的索引为0

【数据结构与算法】Java制作一个简单数组类的更多相关文章

  1. java制作一个简单的抽签程序

    首先需要导入import java.util.Random;才能使用随机类Random:Random生成随机数介绍:https://www.cnblogs.com/prodigal-son/p/128 ...

  2. 用Java制作一个简单的图片验证码

    //Java实现简单验证码功能 package project; import java.awt.Color; import java.awt.Font;import java.awt.Graphic ...

  3. 数据结构和算法(Golang实现)(1)简单入门Golang-前言

    数据结构和算法在计算机科学里,有非常重要的地位.此系列文章尝试使用 Golang 编程语言来实现各种数据结构和算法,并且适当进行算法分析. 我们会先简单学习一下Golang,然后进入计算机程序世界的第 ...

  4. 数据结构和算法(Golang实现)(2)简单入门Golang-包、变量和函数

    包.变量和函数 一.举个例子 现在我们来建立一个完整的程序main.go: // Golang程序入口的包名必须为 main package main // import "golang&q ...

  5. 数据结构和算法(Golang实现)(6)简单入门Golang-并发、协程和信道

    并发.协程和信道 Golang语言提供了go关键字,以及名为chan的数据类型,以及一些标准库的并发锁等,我们将会简单介绍一下并发的一些概念,然后学习这些Golang特征知识. 一.并发介绍 我们写程 ...

  6. 数据结构与算法 java描述 第一章 算法及其复杂度

    目录 数据结构与算法 java描述 笔记 第一章 算法及其复杂度 算法的定义 算法性能的分析与评价 问题规模.运行时间及时间复杂度 渐进复杂度 大 O 记号 大Ω记号 Θ记号 空间复杂度 算法复杂度及 ...

  7. 数据结构和算法(Golang实现)(3)简单入门Golang-流程控制语句

    流程控制语句 计算机编程语言中,流程控制语句很重要,可以让机器知道什么时候做什么事,做几次.主要有条件和循环语句. Golang只有一种循环:for,只有一种判断:if,还有一种特殊的switch条件 ...

  8. 数据结构和算法(Golang实现)(4)简单入门Golang-结构体和方法

    结构体和方法 一.值,指针和引用 我们现在有一段程序: package main import "fmt" func main() { // a,b 是一个值 a := 5 b : ...

  9. 数据结构和算法(Golang实现)(5)简单入门Golang-接口

    接口 在Golang世界中,有一种叫interface的东西,很是神奇. 一.数据类型 interface{} 如果你事前并不知道变量是哪种数据类型,不知道它是整数还是字符串,但是你还是想要使用它. ...

随机推荐

  1. SpringBoot + Layui + JustAuth +Mybatis-plus实现可第三方登录的简单后台管理系统

    1. 简介   在之前博客:SpringBoot基于JustAuth实现第三方授权登录 和 SpringBoot + Layui +Mybatis-plus实现简单后台管理系统(内置安全过滤器)上改造 ...

  2. JDK下载与安装

    Java有很多个版本,最新的版本会兼容之前的. 先附上下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downlo ...

  3. MetaException(message:Could not connect to meta store using any of the URIs provided. Most recent failure: org.apache.thrift.transport.TTransportException: java.net.ConnectException: 拒绝连接 (Connection

    hive在hive-site.xml中配置hive.metastore.uris属性,后启动异常 hive异常 [fan@master hive-0.13.1-cdh5.3.6]$ bin/hive ...

  4. js上 十五、数组-1

    十五.数组-1 #1.什么是数组 组:由多个成员构成的一个集体. 数组:数组是值的有序集合 值就是前面所讲过的这些数据(各种数据类型的都可以) 是数组中,每一个值(如100,'js',true)都称之 ...

  5. 初级程序需要掌握的SQL(一)

    之前我也是,是一个看视频学习的小白,以前老是喜欢通宵看视频,一天10小时小时的学习量,一点效率都没有,就想写一个博客,来帮助大家回顾的SQL语句, 因为我也是初级,所以名字就叫初级程序员需要掌握的sq ...

  6. 面试级解析HashMap

    ------------恢复内容开始------------ 在介绍HashMap之前,有必要先给大家介绍一些参数的概念 HashMap的最大容量,capacity译为容量,capacity就是指Ha ...

  7. 详述网络中ARP安全的综合功能

    组网图形 ARP安全简介 ARP(Address Resolution Protocol)安全是针对ARP攻击的一种安全特性,它通过一系列对ARP表项学习和ARP报文处理的限制.检查等措施来保证网络设 ...

  8. solidworks 2018 因动态绘制边线显示视图延迟的解决方案

    每次鼠标移动到一个物体上时总是会卡顿几秒,直到完成所有边线的绘制后才可以继续进行其他操作,这体验实在是不好. 解决方案很简单,只要取消这个默认开启的动态高亮显示就可以了. 1.去 选项->系统选 ...

  9. HCIP --- BGP实验

    实验拓扑: 要求: R1.R2是EBGP关系,R2.R4是IBGP关系,R4.R5是EBGP邻居关系 R1与R5的环回可以通信 1.配置IP地址 2.BGP承载与IGP之上,所以给AS 2 启用IGP ...

  10. Windows系统提示:“windows找不到文件请确定文件名是否正确后

    最近使用Win7/10系统的用户反应在系统中移动了桌面上的一些与系统无关的文档,在挪动了文件之后出现的问题,弹出了windows找不到文件请确定文件名是否正确后,再试一次, 的错误提示,该怎么办呢? ...