日撸Java三百行 day11-12(顺序表)

2023-11-06 14:50
文章标签 java day11 顺序 三百

本文主要是介绍日撸Java三百行 day11-12(顺序表),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 知识点

1.1 final 的使用

final关键字常用来修饰类、方法和引用。

修饰类:

格式:public final class 类名称 { // ··· }

该类不能有任何的子类,且其所有的成员方法都无法覆盖重写。

修饰方法:

格式:修饰符 final 返回值类型 方法名称(参数列表) { // 方法体 }

该方法就是最终方法,不能被覆盖重写。

修饰引用:

格式: final 引用类型 名称 = 值;

当引用类型为基本数据类型时,则引用为常量,其值无法修改;当引用类型为引用数据类型时,可修改其本身内容,但不能修改指向其的引用;当引用类型为类的成员变量时,由于成员变量具有默认值,所以final之后必须当场赋值。

这里使用到的是修饰一个基础数据类型的全局变量,final后其为常量,不能被修改。

public static final int MAX_LENGTH = 10;

1.2 new关键字的使用

 在Java中任何变量使用前都需要设置初值,Java提供了为类的成员变量赋予初值的功能:构造方法。

构造方法特殊性:

1) 构造方法名字必须与定义它的类名相同,没有返回类型。

2) 其调用是在创建一个对象时用new操作进行的,作用是初始化对象。

3) 每个类有可以有多个或没有构造方法。

4) 不能被static、final、synchronized、abstracth和native修饰,且不能被子类继承

然后再用new关键字加上构造方法来,创建一个对象。

比如:

public class SequentialList {public SequentialList() {}// Of the Constructor
}
在这里SequentialList tempFirstList = new SequentialList(),前面就是在内存中分配一个SequentialList类型的变量tempFirstList,后半部是用new加构造方法,其会在内存中创建一个SequentialList()类的对象,然后new会返回这个对象的地址,最后储存到tempFirstList中。

1.3 方法的Override

方法的重写是封装的特性之一,子类可以对在基类中的继承来的方法进行重写,从而扩充方法以达到自己的需求。但重写方法必须与被重写的方法的名称、参数列表、返回值类型相同,且不能使用比被重写的方法更严格的访问权限。

/************************ Overrides the method claimed in Object, the superclass of any class.**********************/
public String toString() {String resultString = "";if (length == 0) {return "empty";} // Of iffor (int i = 0; i < length - 1; i++) {resultString += data[i] + ", ";} // Of for iresultString += data[length - 1];return resultString;
}// Of toString

这里顺便提一下public作用及有无static的区别:

public是访问修饰符(详解),主要做权限说明,表明该类或者该方法等是公共的,任何程序集都可以去调用到它。

用static修饰的方法,不可调用该类中的非静态成员和非静态方法;其不需要生成示例对象就可以直接调用成员,而且该静态方法(或变量)被该类创建的对象共享;其不能使用super对父类中的成员进行调用。不使用static的非静态方法调用方法或变量时没有限制。

2. 总代码

2.1顺序表的初始化和重置

package datastructure.list;/*** Sequential list.* * @author Yunhua Hu yunhuahu0528@163.com.*/
public class SequentialList {/*** The maximal length of the list. It is a constant.*/public static final int MAX_LENGTH = 10;/*** The actual length not exceeding MAX_LENGTH. Attention: length is not only the* number variable of Sequential list, but also the member variable of Array. In* fact, a name can be the member variable of different classes.*/int length;/*** The data stored in an array.*/int[] data;/*********************** * Construct an empty sequential list********************* */public SequentialList() {length = 0;data = new int[MAX_LENGTH];}// Of the first constructor/*********************** * Construct a sequential list using an array.* * @param paraArray*            The given array. Its length should not exceed MAX_LENGTH. For*            simplicity now we do not check it.********************* */public SequentialList(int[] paraArray) {data = new int[MAX_LENGTH];length = paraArray.length;// Copy data.for (int i = 0; i < paraArray.length; i++) {data[i] = paraArray[i];} // Of for i}// Of the second constructor/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {String resultString = "";if (length == 0) {return "empty";} // Of iffor (int i = 0; i < length - 1; i++) {resultString += data[i] + ",";} // Of for iresultString += data[length - 1];return resultString;}// Of toString/************************ Reset to empty.**********************/public void reset() {length = 0;}// Of reset/************************ The entrance of the progarm.** @param args*            Not used now.**********************/public static void main(String args[]) {int[] tempArray = { 1, 4, 6, 9 };SequentialList tempFirstList = new SequentialList(tempArray);System.out.println("Initialized, the list is: " + tempFirstList.toString());System.out.println("Again, the list is: " + tempFirstList);tempFirstList.reset();System.out.println("After reset, the list is: " + tempFirstList);}// Of main}// Of class SequentialList

输出:

2.2 顺序表的增删查改

对顺序表的增删查改,一定记得考虑false的情况。

package datastructure.list;/*** Sequential list.* * @author Yunhua Hu yunhuahu0528@163.com.*/
public class SequentialList {/*** The maximal length of the list. It is a constant.*/public static final int MAX_LENGTH = 10;/*** The actual length not exceeding MAX_LENGTH. Attention: length is not only the* number variable of Sequential list, but also the member variable of Array. In* fact, a name can be the member variable of different classes.*/int length;/*** The data stored in an array.*/int[] data;/*********************** * Construct an empty sequential list********************* */public SequentialList() {length = 0;data = new int[MAX_LENGTH];}// Of the first constructor/*********************** * Construct a sequential list using an array.* * @param paraArray*            The given array. Its length should not exceed MAX_LENGTH. For*            simplicity now we do not check it.********************* */public SequentialList(int[] paraArray) {data = new int[MAX_LENGTH];length = paraArray.length;// Copy data.for (int i = 0; i < paraArray.length; i++) {data[i] = paraArray[i];} // Of for i}// Of the second constructor/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {String resultString = "";if (length == 0) {return "empty";} // Of iffor (int i = 0; i < length - 1; i++) {resultString += data[i] + ",";} // Of for iresultString += data[length - 1];return resultString;}// Of toString/************************ Reset to empty.**********************/public void reset() {length = 0;}// Of reset/************************ Find the index of the given value. If it appears in multiple positions,* simply return the first one.** @param paraValue The given value.* @return The position. -1 for not found.**********************/public int indexoOf(int paraValue) {int tempPosition = -1;for (int i = 0; i < length; i++) {if (data[i] == paraValue) {tempPosition = i;break;} // Of if} // Of for ireturn tempPosition;}// Of indexOfpublic boolean insert(int paraPosition, int paraValue) {if (length == MAX_LENGTH) {System.out.println("List full.");return false;} // Of ifif ((paraPosition < 0) || (paraPosition > length)) {System.out.println("The position " + paraPosition + " is out of bounds.");return false;} // Of if// Form tail to head. The last one is moved to a new position. Because length <// MAX_LENGTH, no exceeding occurs.for (int i = length; i > paraPosition; i--) {data[i] = data[i - 1];} // Of for idata[paraPosition] = paraValue;length++;return true;}// Of insertpublic boolean delete(int paraPosition) {if ((paraPosition < 0) || (paraPosition >= length)) {System.out.println("The position " + paraPosition + " is out of bounds.");return false;} // Of if// Form head to tail.for (int i = paraPosition; i < length - 1; i++) {data[i] = data[i + 1];} // Of for ilength--;return true;}// Of delete/************************ The entrance of the program.** @param args*            Not used now.**********************/public static void main(String args[]) {int[] tempArray = { 1, 4, 6, 9 };SequentialList tempFirstList = new SequentialList(tempArray);System.out.println("After initialization, the list is: " + tempFirstList.toString());System.out.println("Agian, the list is: " + tempFirstList);int tempValue = 4;int tempPosition = tempFirstList.indexoOf(tempValue);System.out.println("The position of " + tempValue + " is " + tempPosition);tempValue = 5;tempPosition = tempFirstList.indexoOf(tempValue);System.out.println("The position of " + tempValue + " is " + tempPosition);tempPosition = 2;tempValue = 5;tempFirstList.insert(tempPosition, tempValue);System.out.println("After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);tempPosition = 8;tempValue = 10;tempFirstList.insert(tempPosition, tempValue);System.out.println("After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);tempPosition = 3;tempFirstList.delete(tempPosition);System.out.println("After deleting data at position " + tempPosition + ", the list is: " + tempFirstList);for (int i = 0; i < 8; i++) {tempFirstList.insert(i, i);System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList);} // Of for itempFirstList.reset();System.out.println("After reset, the list is: " + tempFirstList);}// Of main}// Of class SequentialList

输出:

这篇关于日撸Java三百行 day11-12(顺序表)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/357269

相关文章

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.