【數(shù)據(jù)結(jié)構(gòu)】順序表-創(chuàng)新互聯(lián)

目錄

創(chuàng)新互聯(lián)公司主要業(yè)務(wù)有網(wǎng)站營(yíng)銷策劃、網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、微信公眾號(hào)開發(fā)、微信平臺(tái)小程序開發(fā)、H5高端網(wǎng)站建設(shè)、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過(guò)程中,公司還積累了豐富的行業(yè)經(jīng)驗(yàn)、成都營(yíng)銷網(wǎng)站建設(shè)資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。 

👀一.初識(shí)順序表

👺二.順序表表的實(shí)現(xiàn)

1. 順序表用數(shù)組elem實(shí)現(xiàn)存放元素,以及usedSize來(lái)記錄使用空間,可以自行設(shè)置默認(rèn)容量大小。

2.在末尾新增元素add(int data)

3.在pos位置新增元素add(int pos ,int data)

4.判斷是否包含某個(gè)元素contains(int toFind)

5.找到某元素對(duì)應(yīng)位置indexOf(int toFind)

6.獲取pos位置元素get(int pos)

7.給pos位置元素設(shè)置為value set(int pos, int value)

8.刪除第一次出現(xiàn)的關(guān)鍵字toRmove remove(int toRmove)

9.獲取順序表長(zhǎng)度 getSize()

10.清空順序表clear()

整體代碼展示


👀一.初識(shí)順序表 順序表是線性表中的一種順序表是用一段 物理地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)數(shù)據(jù)元素的線性結(jié)構(gòu),一般情況下采用數(shù)組存儲(chǔ)。在數(shù)組上完成數(shù)據(jù)的增刪查改。 👺二.順序表表的實(shí)現(xiàn)

提前準(zhǔn)備2個(gè)方法能封裝盡量封裝(當(dāng)養(yǎng)成好習(xí)慣)一個(gè)擴(kuò)容方法 一個(gè)判斷是否已滿

private void reSize(){
        this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
    }
    public boolean isFull(){
        return usedSize == elem.length;
    }
1. 順序表用數(shù)組elem實(shí)現(xiàn)存放元素,以及usedSize來(lái)記錄使用空間,可以自行設(shè)置默認(rèn)容量大小。
public class MyArrayList {
    public int[] elem;//用一個(gè)數(shù)組儲(chǔ)存元素
    public int usedSize;// 記錄已使用空間
    public static final int DEFAULT_SIZE=10; //給定默認(rèn)空間大小
    public MyArrayList(){
        this.elem=new int[DEFAULT_SIZE];
    }
}
2.在末尾新增元素add(int data)
// 新增元素,默認(rèn)在數(shù)組最后新增
    public void add(int data) {
        if (isFull()){
            reSize();
        }
        this.elem[usedSize]=data;
        usedSize++;
    }
3.在pos位置新增元素add(int pos ,int data)
public void add(int pos, int data) {
        checkIndex(pos);
        if (isFull()){
            reSize();
        }
        for (int i=usedSize-1;i>=pos;i--){
            elem[i+1]=elem[i];//從pos開始每個(gè)數(shù)往后挪一個(gè)位置
        }
        elem[pos]=data;
        usedSize++;
    }
private void checkGetIndex(int pos){
        if (pos>=usedSize||pos<0){
            throw new IndexOutOfBoundException("get元素時(shí)位置不合法");
        }
    }
4.判斷是否包含某個(gè)元素contains(int toFind)
// 判定是否包含某個(gè)元素
    public boolean contains(int toFind) {
       for (int i=0;i
5.找到某元素對(duì)應(yīng)位置indexOf(int toFind)
// 查找某個(gè)元素對(duì)應(yīng)的位置
    public int indexOf(int toFind) {
        for (int i=0;i
6.獲取pos位置元素get(int pos)
// 獲取 pos 位置的元素
    public int get(int pos) {
    checkIndex(pos);
        return this.elem[pos]; }
private void checkIndex(int pos){
        if (pos>usedSize||pos<0){
            throw new IndexOutOfBoundException("add元素時(shí)位置不合法");
        }
    }
7.給pos位置元素設(shè)置為value set(int pos, int value)
// 給 pos 位置的元素設(shè)為 value
    public void set(int pos, int value) {
        checkIndex(pos);
        this.elem[pos]=value;
    }
8.刪除第一次出現(xiàn)的關(guān)鍵字toRmove remove(int toRmove)
//刪除第一次出現(xiàn)的關(guān)鍵字key
    public boolean remove(int toRemove) {
        if (indexOf(toRemove)==-1){
            System.out.println("沒找到這個(gè)數(shù)");
            return false;
        }
        int index=indexOf(toRemove);
        for (int i=index;i
9.獲取順序表長(zhǎng)度 getSize()
public int getSize() {
        return usedSize; }
10.清空順序表clear()
// 清空順序表
    public void clear() {
        usedSize=0;
    }
整體代碼展示
import java.util.Arrays;

public class MyArrayList {
    public int[] elem;//用一個(gè)數(shù)組儲(chǔ)存元素
    public int usedSize;// 記錄已使用空間
    public static final int DEFAULT_SIZE=10; //給定默認(rèn)空間大小
    public MyArrayList(){
        this.elem=new int[DEFAULT_SIZE];
    }
    // 新增元素,默認(rèn)在數(shù)組最后新增
    public void add(int data) {
        if (isFull()){
            reSize();
        }
        this.elem[usedSize]=data;
        usedSize++;
    }
    private void reSize(){
        this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
    }
    public boolean isFull(){
        return usedSize == elem.length;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        checkIndex(pos);
        if (isFull()){
            reSize();
        }
        for (int i=usedSize-1;i>=pos;i--){
            elem[i+1]=elem[i];
        }
        elem[pos]=data;
        usedSize++;
    }
    private void checkIndex(int pos){
        if (pos>usedSize||pos<0){
            throw new IndexOutOfBoundException("add元素時(shí)位置不合法");
        }
    }
    private void checkGetIndex(int pos){
        if (pos>=usedSize||pos<0){
            throw new IndexOutOfBoundException("get元素時(shí)位置不合法");
        }
    }
    // 判定是否包含某個(gè)元素
    public boolean contains(int toFind) {
       for (int i=0;i

本人第一次寫博客,主要為學(xué)習(xí)記錄使用,如有錯(cuò)誤和不好的地方歡迎大家指出。

最后謝謝大家的閱讀。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)站標(biāo)題:【數(shù)據(jù)結(jié)構(gòu)】順序表-創(chuàng)新互聯(lián)
當(dāng)前地址:http://www.bm7419.com/article28/dpcicp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)品牌網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、定制網(wǎng)站、企業(yè)建站、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)