日撸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 SE包装类和泛型详细介绍及说明方法

《JAVASE包装类和泛型详细介绍及说明方法》:本文主要介绍JAVASE包装类和泛型的相关资料,包括基本数据类型与包装类的对应关系,以及装箱和拆箱的概念,并重点讲解了自动装箱和自动拆箱的机制,文... 目录1. 包装类1.1 基本数据类型和对应的包装类1.2 装箱和拆箱1.3 自动装箱和自动拆箱2. 泛型2

SpringBoot操作MaxComputer方式(保姆级教程)

《SpringBoot操作MaxComputer方式(保姆级教程)》:本文主要介绍SpringBoot操作MaxComputer方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的... 目录引言uqNqjoe一、引入依赖二、配置文件 application.properties(信息用自己

JavaScript中的Map用法完全指南

《JavaScript中的Map用法完全指南》:本文主要介绍JavaScript中Map用法的相关资料,通过实例讲解了Map的创建、常用方法和迭代方式,还探讨了Map与对象的区别,并通过一个例子展... 目录引言1. 创建 Map2. Map 和对象的对比3. Map 的常用方法3.1 set(key, v

Javascript访问Promise对象返回值的操作方法

《Javascript访问Promise对象返回值的操作方法》这篇文章介绍了如何在JavaScript中使用Promise对象来处理异步操作,通过使用fetch()方法和Promise对象,我们可以从... 目录在Javascript中,什么是Promise1- then() 链式操作2- 在之后的代码中使

JAVA虚拟机中 -D, -X, -XX ,-server参数使用

《JAVA虚拟机中-D,-X,-XX,-server参数使用》本文主要介绍了JAVA虚拟机中-D,-X,-XX,-server参数使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录一、-D参数二、-X参数三、-XX参数总结:在Java开发过程中,对Java虚拟机(JVM)的启动参数进

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

Java中使用注解校验手机号格式的详细指南

《Java中使用注解校验手机号格式的详细指南》在现代的Web应用开发中,数据校验是一个非常重要的环节,本文将详细介绍如何在Java中使用注解对手机号格式进行校验,感兴趣的小伙伴可以了解下... 目录1. 引言2. 数据校验的重要性3. Java中的数据校验框架4. 使用注解校验手机号格式4.1 @NotBl

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

SpringBoot基于沙箱环境实现支付宝支付教程

《SpringBoot基于沙箱环境实现支付宝支付教程》本文介绍了如何使用支付宝沙箱环境进行开发测试,包括沙箱环境的介绍、准备步骤、在SpringBoot项目中结合支付宝沙箱进行支付接口的实现与测试... 目录一、支付宝沙箱环境介绍二、沙箱环境准备2.1 注册入驻支付宝开放平台2.2 配置沙箱环境2.3 沙箱

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现