日撸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

相关文章

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java内存分配与JVM参数详解(推荐)

《Java内存分配与JVM参数详解(推荐)》本文详解JVM内存结构与参数调整,涵盖堆分代、元空间、GC选择及优化策略,帮助开发者提升性能、避免内存泄漏,本文给大家介绍Java内存分配与JVM参数详解,... 目录引言JVM内存结构JVM参数概述堆内存分配年轻代与老年代调整堆内存大小调整年轻代与老年代比例元空

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操