Java數(shù)據(jù)結構之順序表的實現(xiàn)
前言
線性表(linear list)是n個具有相同特性的數(shù)據(jù)元素的有限序列。 線性表是一種在實際中廣泛使用的數(shù)據(jù)結構,常見 的線性表:順序表、鏈表、棧、隊列、字符串… 線性表在邏輯上是線性結構,也就說是連續(xù)的一條直線。但是在物理結構上并不一定是連續(xù)的,線性表在物理上存儲 時,通常以數(shù)組和鏈式結構的形式存儲。

一、順序表
1.1 什么是順序表
順序表是用一段物理地址連續(xù)的存儲單元依次存儲數(shù)據(jù)元素的線性結構,一般情況下采用數(shù)組存儲。在數(shù)組上完成數(shù)據(jù)的增刪查改 。
其實就是一個數(shù)組。那為什么還要寫一個順序表,直接用數(shù)組不就好了?不一樣的,寫到類里面就可以面向對象。
順序表一般可以分為:
- 靜態(tài)順序表:使用定長數(shù)組存儲
- 動態(tài)順序表:使用動態(tài)開辟的數(shù)組存儲
靜態(tài)順序表適用于確定知道需要存多少數(shù)據(jù)的場景.
靜態(tài)順序表的定長數(shù)組導致N定大了,空間開多了浪費,開少了不夠用.
相比之下動態(tài)順序表更靈活, 根據(jù)需要動態(tài)的分配空間大小.
二、簡單實現(xiàn)順序表
2.1 創(chuàng)建順序表
public class MyArrayList {
public int[] elem;//數(shù)組
public int usedSize;//數(shù)據(jù)的有效個數(shù)
public MyArrayList(){
this.elem = new int[10];
}
}

2.2 打印順序表
//打印順序表
public void display(){
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
2.3 獲取順序表長度
//獲取順序表長度
public int size(){
return this.usedSize;
}
2.4 在 pos 位置新增元素
在順序表里面插入元素的時候所插入的位置的前面一定是存放了元素的

//在 pos 位置新填元素
public void add(int pos,int data){
if(pos < 0 || pos >usedSize){
System.out.println("pos 位置不合法!");
return;
}
if(isfull()) {
Arrays.copyOf(this.elem,2*this.elem.length);
}
for (int i = this.usedSize - 1; i >= pos; i--) {
this.elem[i + 1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
//判斷是否滿
public boolean isfull(){
return this.usedSize == this.elem.length;
}2.5 判定是否包含某個元素
//判斷是否包含某個元素
public boolean contains(int toFind){
for (int i = 0; i < this.usedSize; i++) {
if(this.elem[i] == toFind){
return true;
}
}
return false;
}
2.6 查找某個元素對應的位置
//查找某個元素的對應位置,找不到返回-1
public int search(int toFind){
for (int i = 0; i < this.usedSize; i++) {
if(this.elem[i] == toFind){
return i;
}
}
return -1;
}
2.7 獲取 pos 位置的元素
//獲取pos位置的值
public int getPos(int pos){
if(pos < 0 || pos >= this.usedSize){
System.out.println("pos 位置不合法");
return -1;//這里說明一下,業(yè)務上的處理,不考慮
}
if(isEmpty()){
System.out.println("順序表為空!");
return -1;
}
return this.elem[pos];
}
public boolean isEmpty(){
return this.usedSize == 0;
}
2.8 給 pos 位置的元素設為 value
//給pos位置元素更新value
public void setPos(int pos,int value){
if (pos < 0 || pos >= this.usedSize){
System.out.println("pos 位置不合法");
return;
}
if(isEmpty()){
System.out.println("順序表為空!");
return;
}
this.elem[pos] = value;
}
2.9 刪除你想要刪除的元素
//刪除第一次出現(xiàn)的關鍵字key
public void remove(int toRmove){
if (isEmpty()){
System.out.println("順序表為空!");
return;
}
int index = search(toRmove);
if(index == -1){
System.out.println("沒有你要刪除的數(shù)字!");
return;
}
for (int i = index; i < this.usedSize - 1; i++) {
this.elem[i] = this.elem[i+1];
}
this.usedSize--;
//this.elem[useSize] = null;如果數(shù)組當中是引用數(shù)據(jù)類型
}2.10 清空順序表
//清空順序表
public void clear(){
this.usedSize = 0;
}
三、MyArrayList.java
import java.util.Arrays;
public class MyArrayList {
public int[] elem;
public int usedSize;
public MyArrayList(){
this.elem = new int[10];
}
//打印順序表
public void display(){
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
//獲取順序表長度
public int size(){
return this.usedSize;
}
//在 pos 位置新填元素
public void add(int pos,int data){
if(pos < 0 || pos >usedSize){
System.out.println("pos 位置不合法!");
return;
}
if(isfull()) {
Arrays.copyOf(this.elem,2*this.elem.length);
}
for (int i = this.usedSize - 1; i >= pos; i--) {
this.elem[i + 1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
//判斷是否滿
public boolean isfull(){
return this.usedSize == this.elem.length;
}
//判斷是否包含某個元素
public boolean contains(int toFind){
for (int i = 0; i < this.usedSize; i++) {
if(this.elem[i] == toFind){
return true;
}
}
return false;
}
//查找某個元素的對應位置,找不到返回-1
public int search(int toFind){
for (int i = 0; i < this.usedSize; i++) {
if(this.elem[i] == toFind){
return i;
}
}
return -1;
}
//獲取pos位置的值
public int getPos(int pos){
if(pos < 0 || pos >= this.usedSize){
System.out.println("pos 位置不合法");
return -1;//這里說明一下,業(yè)務上的處理,不考慮
}
if(isEmpty()){
System.out.println("順序表為空!");
return -1;
}
return this.elem[pos];
}
public boolean isEmpty(){
return this.usedSize == 0;
}
//給pos位置元素更新value
public void setPos(int pos,int value){
if (pos < 0 || pos >= this.usedSize){
System.out.println("pos 位置不合法");
return;
}
if(isEmpty()){
System.out.println("順序表為空!");
return;
}
this.elem[pos] = value;
}
//刪除第一次出現(xiàn)的關鍵字key
public void remove(int toRmove){
if (isEmpty()){
System.out.println("順序表為空!");
return;
}
int index = search(toRmove);
if(index == -1){
System.out.println("沒有你要刪除的數(shù)字!");
return;
}
for (int i = index; i < this.usedSize - 1; i++) {
this.elem[i] = this.elem[i+1];
}
this.usedSize--;
//this.elem[useSize] = null;如果數(shù)組當中是引用數(shù)據(jù)類型
}
//清空順序表
public void clear(){
this.usedSize = 0;
}
}
四、Test.java
public class Test {
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
myArrayList.add(0,1);
myArrayList.add(1,2);
myArrayList.add(2,3);
myArrayList.add(3,4);
myArrayList.add(4,5);
myArrayList.display();
System.out.println(myArrayList.contains(3));
System.out.println(myArrayList.getPos(3));
myArrayList.setPos(0,99);
myArrayList.display();
}
}以上就是Java數(shù)據(jù)結構之順序表的實現(xiàn)的詳細內容,更多關于Java順序表的資料請關注腳本之家其它相關文章!
相關文章
Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程
這篇文章主要為大家介紹了Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
Spring Security 和Apache Shiro你需要具備哪些條件
這篇文章主要介紹了Spring Security 和Apache Shiro你需要具備哪些條件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
java連接mysql數(shù)據(jù)庫及測試是否連接成功的方法
這篇文章主要介紹了java連接mysql數(shù)據(jù)庫及測試是否連接成功的方法,結合完整實例形式分析了java基于jdbc連接mysql數(shù)據(jù)庫并返回連接狀態(tài)的具體步驟與相關操作技巧,需要的朋友可以參考下2017-09-09
Selenium+Tesseract-OCR智能識別驗證碼爬取網頁數(shù)據(jù)的實例
本文主要介紹了Selenium+Tesseract-OCR智能識別驗證碼爬取網頁數(shù)據(jù),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09

