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

數組不就是一個現場的順序表嗎?但是數組并沒有直接向我們提供增刪查改的工具,所以我們必須重新實現一下順序表。
二、自定義異常
空引用異常
如果我們的順序表為空時,手動拋出空引用異常
public class NullException extends RuntimeException{
public NullException(String message) {
super(message);
}
}
下標越界異常
當我們進行增刪查改時,下標越界時,我們手動拋出一個下標越界異常
public class IndexException extends RuntimeException{
public IndexException(String message) {
super(message);
}
}
三、順序表的方法
順序表的實現
這里我們定義一個順序表,默認容量為DEFAULTSIZE,實際大小為usedsize.
public class ArrList {
public int[] arr;
public int usedSize;
public static final int DEFAULTSIZE = 10;
public ArrList() {
this.arr = new int[DEFAULTSIZE];
}
}
獲取順序表長度
usedSize存儲的就是當前順序表的長度,直接返回即可。
public int size() {
return this.usedSize;
}
順序表是否為空
此方法我們只想在順序表內部使用,所以我們定義為private.
private boolean isEmpty() {
return this.arr == null;
}
順序表是否為滿
此方法我們只想在順序表內部使用,所以我們定義為private.
private boolean isFull() {
//如果數組所放元素大于等于數組長度,那么數組滿了
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.數組為空,報空異常
if(isEmpty()) {
throw new NullException("數組為空");
}
//2.數組滿了,先增容
if(isFull()) {
this.arr = new int[2 * this.arr.length];
}
//3.進行新增
this.arr[this.usedSize] = data;
//4.元素+1
this.usedSize++;
}
指定位置新增元素
public void add(int pos, int data) throws RuntimeException,IndexException{
//1.判斷數組是否為空
if(isEmpty()) {
throw new NullException("數組為空");
}
//2.判斷新增位置是否合法,拋數組越界異常
if(pos < 0 || pos > this.arr.length) {
throw new IndexException("數組越界");
}
//3.判斷數組是否已滿,進行擴容
if(isFull()) {
this.arr = new int[2 * this.arr.length];
}
//4.進行新增
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;
}
查找某個元素對應的位置
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位置數組越界");
}else {
return this.arr[pos];
}
}
給 pos 位置的元素賦值
public void set(int pos, int value) throws NullException,IndexException{
if(isEmpty()) {
throw new NullException("數組為空");
}
//2.判斷新增位置是否合法,拋數組越界異常
if(pos < 0 || pos >= this.arr.length) {
throw new IndexException("數組越界");
}
this.arr[pos] = value;
}
刪除第一次出現的關鍵字key
public void remove(int toRemove) throws NullException{
if(isEmpty()) {
throw new NullException("數組為空");
}
int ret = indexOf(toRemove);
if(ret == -1) {
System.out.println("不存在此數");
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();
}
// 新增元素,默認在數組最后新增
public void add(int data) throws NullException{
//1.數組為空,報空異常
if(isEmpty()) {
throw new NullException("數組為空");
}
//2.數組滿了,先增容
if(isFull()) {
this.arr = new int[2 * this.arr.length];
}
//3.進行新增
this.arr[this.usedSize] = data;
//4.元素+1
this.usedSize++;
}
private boolean isFull() {
//如果數組所放元素大于等于數組長度,那么數組滿了
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.判斷數組是否為空
if(isEmpty()) {
throw new NullException("數組為空");
}
//2.判斷新增位置是否合法,拋數組越界異常
if(pos < 0 || pos > this.arr.length) {
throw new IndexException("數組越界");
}
//3.判斷數組是否已滿,進行擴容
if(isFull()) {
this.arr = new int[2 * this.arr.length];
}
//4.進行新增
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;
}
// 查找某個元素對應的位置
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位置數組越界");
}else {
return this.arr[pos];
}
}
// 給 pos 位置的元素設為 value
public void set(int pos, int value) throws NullException,IndexException{
if(isEmpty()) {
throw new NullException("數組為空");
}
//2.判斷新增位置是否合法,拋數組越界異常
if(pos < 0 || pos >= this.arr.length) {
throw new IndexException("數組越界");
}
this.arr[pos] = value;
}
//刪除第一次出現的關鍵字key
public void remove(int toRemove) throws NullException{
if(isEmpty()) {
throw new NullException("數組為空");
}
int ret = indexOf(toRemove);
if(ret == -1) {
System.out.println("不存在此數");
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實現順序表的操作詳解的詳細內容,更多關于Java順序表的資料請關注腳本之家其它相關文章!
相關文章
springboot使用之多個filter的執(zhí)行順序以及配置方式
這篇文章主要介紹了springboot使用之多個filter的執(zhí)行順序以及配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
java 發(fā)送帶Basic Auth認證的http post請求實例代碼
下面小編就為大家?guī)硪黄猨ava 發(fā)送帶Basic Auth認證的http post請求實例代碼。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11

