Java實(shí)現(xiàn)順序表的操作詳解
一、順序表是什么
順序表是用一段物理地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)數(shù)據(jù)元素的線性結(jié)構(gòu),一般情況下采用數(shù)組存儲(chǔ)。在數(shù)組上完成數(shù)據(jù)的增刪查改。

數(shù)組不就是一個(gè)現(xiàn)場的順序表嗎?但是數(shù)組并沒有直接向我們提供增刪查改的工具,所以我們必須重新實(shí)現(xiàn)一下順序表。
二、自定義異常
空引用異常
如果我們的順序表為空時(shí),手動(dòng)拋出空引用異常
public class NullException extends RuntimeException{
public NullException(String message) {
super(message);
}
}
下標(biāo)越界異常
當(dāng)我們進(jìn)行增刪查改時(shí),下標(biāo)越界時(shí),我們手動(dòng)拋出一個(gè)下標(biāo)越界異常
public class IndexException extends RuntimeException{
public IndexException(String message) {
super(message);
}
}
三、順序表的方法
順序表的實(shí)現(xiàn)
這里我們定義一個(gè)順序表,默認(rèn)容量為DEFAULTSIZE,實(shí)際大小為usedsize.
public class ArrList {
public int[] arr;
public int usedSize;
public static final int DEFAULTSIZE = 10;
public ArrList() {
this.arr = new int[DEFAULTSIZE];
}
}
獲取順序表長度
usedSize存儲(chǔ)的就是當(dāng)前順序表的長度,直接返回即可。
public int size() {
return this.usedSize;
}
順序表是否為空
此方法我們只想在順序表內(nèi)部使用,所以我們定義為private.
private boolean isEmpty() {
return this.arr == null;
}
順序表是否為滿
此方法我們只想在順序表內(nèi)部使用,所以我們定義為private.
private boolean isFull() {
//如果數(shù)組所放元素大于等于數(shù)組長度,那么數(shù)組滿了
return this.size() >= this.arr.length;
}
打印順序表
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
末尾新增元素
public void add(int data) throws NullException{
//1.數(shù)組為空,報(bào)空異常
if(isEmpty()) {
throw new NullException("數(shù)組為空");
}
//2.數(shù)組滿了,先增容
if(isFull()) {
this.arr = new int[2 * this.arr.length];
}
//3.進(jìn)行新增
this.arr[this.usedSize] = data;
//4.元素+1
this.usedSize++;
}
指定位置新增元素
public void add(int pos, int data) throws RuntimeException,IndexException{
//1.判斷數(shù)組是否為空
if(isEmpty()) {
throw new NullException("數(shù)組為空");
}
//2.判斷新增位置是否合法,拋數(shù)組越界異常
if(pos < 0 || pos > this.arr.length) {
throw new IndexException("數(shù)組越界");
}
//3.判斷數(shù)組是否已滿,進(jìn)行擴(kuò)容
if(isFull()) {
this.arr = new int[2 * this.arr.length];
}
//4.進(jìn)行新增
for (int i = this.usedSize - 1; i >= pos; i--) {
this.arr[i+1] = this.arr[i];
}
this.arr[pos] = data;
this.usedSize++;
}判斷是否包含某元素
public boolean contains(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if(toFind == this.arr[i]) {
return true;
}
}
return false;
}
查找某個(gè)元素對應(yīng)的位置
public int indexOf(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if(toFind == this.arr[i]) {
return i;
}
}
return -1;
}
獲取 pos 位置的元素
public int get(int pos) throws IndexException{
//判斷pos位置是否合法
if(pos < 0 || pos >= this.usedSize) {
throw new IndexException("輸入pos位置數(shù)組越界");
}else {
return this.arr[pos];
}
}
給 pos 位置的元素賦值
public void set(int pos, int value) throws NullException,IndexException{
if(isEmpty()) {
throw new NullException("數(shù)組為空");
}
//2.判斷新增位置是否合法,拋數(shù)組越界異常
if(pos < 0 || pos >= this.arr.length) {
throw new IndexException("數(shù)組越界");
}
this.arr[pos] = value;
}
刪除第一次出現(xiàn)的關(guān)鍵字key
public void remove(int toRemove) throws NullException{
if(isEmpty()) {
throw new NullException("數(shù)組為空");
}
int ret = indexOf(toRemove);
if(ret == -1) {
System.out.println("不存在此數(shù)");
return;
}
if(ret != -1) {
for (int i = ret; i < this.usedSize - 1; i++) {
this.arr[i] = this.arr[i+1];
}
}
this.usedSize++;
}清空順序表
public void clear() {
this.usedSize = 0;
//如果為引用類型
// for (int i = 0; i < size(); i++) {
// this.arr[i] = null;
// }
// this.usedSize = 0;
}
}
四、自定義順序表
public class ArrList {
public int[] arr;
public int usedSize;
public static final int DEFAULTSIZE = 10;
public ArrList() {
this.arr = new int[DEFAULTSIZE];
}
// 打印順序表
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
// 新增元素,默認(rèn)在數(shù)組最后新增
public void add(int data) throws NullException{
//1.數(shù)組為空,報(bào)空異常
if(isEmpty()) {
throw new NullException("數(shù)組為空");
}
//2.數(shù)組滿了,先增容
if(isFull()) {
this.arr = new int[2 * this.arr.length];
}
//3.進(jìn)行新增
this.arr[this.usedSize] = data;
//4.元素+1
this.usedSize++;
}
private boolean isFull() {
//如果數(shù)組所放元素大于等于數(shù)組長度,那么數(shù)組滿了
return this.size() >= this.arr.length;
}
private boolean isEmpty() {
return this.arr == null;
}
// 在 pos 位置新增元素
public void add(int pos, int data) throws RuntimeException,IndexException{
//1.判斷數(shù)組是否為空
if(isEmpty()) {
throw new NullException("數(shù)組為空");
}
//2.判斷新增位置是否合法,拋數(shù)組越界異常
if(pos < 0 || pos > this.arr.length) {
throw new IndexException("數(shù)組越界");
}
//3.判斷數(shù)組是否已滿,進(jìn)行擴(kuò)容
if(isFull()) {
this.arr = new int[2 * this.arr.length];
}
//4.進(jìn)行新增
for (int i = this.usedSize - 1; i >= pos; i--) {
this.arr[i+1] = this.arr[i];
}
this.arr[pos] = data;
this.usedSize++;
}
// 判定是否包含某個(gè)元素
public boolean contains(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if(toFind == this.arr[i]) {
return true;
}
}
return false;
}
// 查找某個(gè)元素對應(yīng)的位置
public int indexOf(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if(toFind == this.arr[i]) {
return i;
}
}
return -1;
}
// 獲取 pos 位置的元素
public int get(int pos) throws IndexException{
//判斷pos位置是否合法
if(pos < 0 || pos >= this.usedSize) {
throw new IndexException("輸入pos位置數(shù)組越界");
}else {
return this.arr[pos];
}
}
// 給 pos 位置的元素設(shè)為 value
public void set(int pos, int value) throws NullException,IndexException{
if(isEmpty()) {
throw new NullException("數(shù)組為空");
}
//2.判斷新增位置是否合法,拋數(shù)組越界異常
if(pos < 0 || pos >= this.arr.length) {
throw new IndexException("數(shù)組越界");
}
this.arr[pos] = value;
}
//刪除第一次出現(xiàn)的關(guān)鍵字key
public void remove(int toRemove) throws NullException{
if(isEmpty()) {
throw new NullException("數(shù)組為空");
}
int ret = indexOf(toRemove);
if(ret == -1) {
System.out.println("不存在此數(shù)");
return;
}
if(ret != -1) {
for (int i = ret; i < this.usedSize - 1; i++) {
this.arr[i] = this.arr[i+1];
}
}
this.usedSize++;
}
// 獲取順序表長度
public int size() {
return this.usedSize;
}
// 清空順序表
public void clear() {
this.usedSize = 0;
//如果為引用類型
// for (int i = 0; i < size(); i++) {
// this.arr[i] = null;
// }
// this.usedSize = 0;
}
}
以上就是Java實(shí)現(xiàn)順序表的操作詳解的詳細(xì)內(nèi)容,更多關(guān)于Java順序表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot使用之多個(gè)filter的執(zhí)行順序以及配置方式
這篇文章主要介紹了springboot使用之多個(gè)filter的執(zhí)行順序以及配置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach
這篇文章主要介紹了MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法
本篇文章主要介紹了Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04
SpringBoot任務(wù)之定時(shí)任務(wù)相關(guān)知識總結(jié)
今天給大家整理的文章是SpringBoot定時(shí)任務(wù)的相關(guān)知識點(diǎn),文中有非常詳細(xì)的介紹及代碼示例,對正在學(xué)習(xí)SpringBoot任務(wù)的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06
java 發(fā)送帶Basic Auth認(rèn)證的http post請求實(shí)例代碼
下面小編就為大家?guī)硪黄猨ava 發(fā)送帶Basic Auth認(rèn)證的http post請求實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Java中super關(guān)鍵字的用法和細(xì)節(jié)
大家好,本篇文章主要講的是Java中super關(guān)鍵字的用法和細(xì)節(jié),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01

