目錄
👀一.初識順序表
👺二.順序表表的實現(xiàn)
1. 順序表用數(shù)組elem實現(xiàn)存放元素,以及usedSize來記錄使用空間,可以自行設(shè)置默認(rèn)容量大小。
2.在末尾新增元素add(int data)
3.在pos位置新增元素add(int pos ,int data)
4.判斷是否包含某個元素contains(int toFind)
5.找到某元素對應(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.獲取順序表長度 getSize()
10.清空順序表clear()
整體代碼展示
提前準(zhǔn)備2個方法能封裝盡量封裝(當(dāng)養(yǎng)成好習(xí)慣)一個擴容方法 一個判斷是否已滿
private void reSize(){
this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
}
public boolean isFull(){
return usedSize == elem.length;
}
1. 順序表用數(shù)組elem實現(xiàn)存放元素,以及usedSize來記錄使用空間,可以自行設(shè)置默認(rèn)容量大小。public class MyArrayList {
public int[] elem;//用一個數(shù)組儲存元素
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開始每個數(shù)往后挪一個位置
}
elem[pos]=data;
usedSize++;
}
private void checkGetIndex(int pos){
if (pos>=usedSize||pos<0){
throw new IndexOutOfBoundException("get元素時位置不合法");
}
}
4.判斷是否包含某個元素contains(int toFind)// 判定是否包含某個元素
public boolean contains(int toFind) {
for (int i=0;i
5.找到某元素對應(yīng)位置indexOf(int toFind)// 查找某個元素對應(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元素時位置不合法");
}
}
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("沒找到這個數(shù)");
return false;
}
int index=indexOf(toRemove);
for (int i=index;i
9.獲取順序表長度 getSize()public int getSize() {
return usedSize; }
10.清空順序表clear()// 清空順序表
public void clear() {
usedSize=0;
}
整體代碼展示import java.util.Arrays;
public class MyArrayList {
public int[] elem;//用一個數(shù)組儲存元素
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元素時位置不合法");
}
}
private void checkGetIndex(int pos){
if (pos>=usedSize||pos<0){
throw new IndexOutOfBoundException("get元素時位置不合法");
}
}
// 判定是否包含某個元素
public boolean contains(int toFind) {
for (int i=0;i
本人第一次寫博客,主要為學(xué)習(xí)記錄使用,如有錯誤和不好的地方歡迎大家指出。
最后謝謝大家的閱讀。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站標(biāo)題:【數(shù)據(jù)結(jié)構(gòu)】順序表-創(chuàng)新互聯(lián)
當(dāng)前地址:http://jinyejixie.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)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容