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. 在线CC攻击网站源码

    源码目录 index.html 首页 cc.php 核心文件 count.php 使用统计 pv.php 访问测试页面 ip.txt 代理IP数据文件 运行方式 域名/?url=目标网址 要先获取代{ ...

  2. css进阶 00-准备

    前言 css 进阶的主要内容如下. #1.css 非布局样式 html 元素的分类和特性 css 选择器 css 常见属性(非布局样式) #2.css 布局相关 css 布局属性和组合解析 常见布局方 ...

  3. [日常摸鱼][poj2777]Count Color-线段树

    辣鸡会考考完啦哈哈哈哈 题意:一块板分成$L$块,每次给一段连续的块染色或者询问一段有几种颜色,颜色的范围$\leq 30$ 我记得我好像做过一个类似的二维染色的问题-不过那个用树状数组直接过掉了- ...

  4. 软件测试最常用的 SQL 命令 | 掌握基本查询、条件查询、聚合查询

    1.DML核心CRUD增删改查 缩写全称和对应 SQL: * DML 数据操纵语言:Data Manipulation Language * Create 增加:insert * Retrieve 查 ...

  5. matplotlib的学习2-基本用法

    import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1, 1, 50)#范围-1 到 1,个数是50 y = 2*x ...

  6. Winform 去掉 最大化 最小化 关闭按钮(不是关闭按钮变灰)终极解决办法

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. 现代JavaScript—ES6+中的Imports,Exports,Let,Const和Promise

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者.原文出处:https://www.freecodecamp.org/news/learn-modern-jav ...

  8. 将notepad++关联到右键菜单

    Step1: 新建txt文本, 将以下内容复制到文本中: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT*\Shell\NotePad+ ...

  9. 使用@Param注解

    1,使用@Param注解 当以下面的方式进行写SQL语句时: @Select("select column from table where userid = #{userid} " ...

  10. IdentityServer4 之Client Credentials走起来

    前言 API裸奔是绝对不允许滴,之前专门针对这块分享了jwt的解决方案(WebApi接口裸奔有风险):那如果是微服务,又怎么解决呢?每一个服务都加认证授权也可以解决问题,只是显得认证授权这块冗余,重复 ...