日撸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中最基础且常用的数据结构之一,其初始化方式多样且各具特点,本文详细讲解Java数组初始化的五种方式,分析其适用场景、优劣势对比及注意事项,帮助避免常见陷阱... 目录1. 静态初始化:简洁但固定代码示例核心特点适用场景注意事项2. 动态初始化:灵活但需手动管理代

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

将Java项目提交到云服务器的流程步骤

《将Java项目提交到云服务器的流程步骤》所谓将项目提交到云服务器即将你的项目打成一个jar包然后提交到云服务器即可,因此我们需要准备服务器环境为:Linux+JDK+MariDB(MySQL)+Gi... 目录1. 安装 jdk1.1 查看 jdk 版本1.2 下载 jdk2. 安装 mariadb(my

SpringBoot中配置Redis连接池的完整指南

《SpringBoot中配置Redis连接池的完整指南》这篇文章主要为大家详细介绍了SpringBoot中配置Redis连接池的完整指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以... 目录一、添加依赖二、配置 Redis 连接池三、测试 Redis 操作四、完整示例代码(一)pom.

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析