
?作者簡介:大家好,我是橘橙黃又青,一個想要與大家共同進(jìn)步的男人????
??個人主頁:再無B~U~G-CSDN博客
?1. 什么是List
在集合框架中,List是一個接口,繼承自Collection。
Collection也是一個接口,該接口中規(guī)范了后序容器中常用的一些方法,具體如下所示:?

List 的官方文檔 ?
2. 常見接口介紹
?List中提供了好的方法,具體如下:
方法
|
解釋 |
boolean
add
(E e)
|
尾插 e |
void
add
(int index, E element)
|
將 e 插入到 index 位置 |
boolean
addAll
(Collection<? extends E> c)
|
尾插 c 中的元素 |
E
remove
(int index)
|
刪除 index 位置元素 |
boolean
remove
(Object o)
|
刪除遇到的第一個 o |
E
get
(int index)
|
獲取下標(biāo) index 位置元素 |
E
set
(int index, E element)
|
將下標(biāo) index 位置元素設(shè)置為 element |
void
clear
()
|
清空 |
boolean
contains
(Object o)
|
判斷 o 是否在線性表中 |
int
indexOf
(Object o)
|
返回第一個 o 所在下標(biāo) |
int
lastIndexOf
(Object o)
|
返回最后一個 o 的下標(biāo) |
List<E>
subList
(int fromIndex, int toIndex)
|
截取部分 list |
3.簡單實現(xiàn)List順序表的底層邏輯
目的:為了更加清楚的了解順序表的使用
相應(yīng)的級別關(guān)系:
把所有的順序表方法都定義在IList接口:
這里不帶多講,結(jié)構(gòu)上跟c語言差不多。
我們說一下異常這一塊:
梳理一下異常的應(yīng)用,比如說:
實現(xiàn)代碼:
相應(yīng)的解釋代碼里面都有:文章來源:http://www.zghlxwxcb.cn/news/detail-861774.html
?src/arrayList/IList接口
package arrayList;
public interface IList {
// 新增元素,默認(rèn)在數(shù)組最后新增
void add(int data);
// 在 pos 位置新增元素
void add(int pos, int data);
// 判定是否包含某個元素
boolean contains(int toFind);
// 查找某個元素對應(yīng)的位置
int indexOf(int toFind);
// 獲取 pos 位置的元素
int get(int pos);
// 給 pos 位置的元素設(shè)為 value -> 更新
void set(int pos, int value);
//刪除第一次出現(xiàn)的關(guān)鍵字key
void remove(int toRemove);
// 獲取順序表長度
int size();
// 清空順序表
void clear();
// 打印順序表,
// 注意:該方法并不是順序表中的方法,為了方便看測試結(jié)果給出的
void display();
}
src/arrayList/MyArrayList類
package arrayList;
import java.util.Arrays;
public class MyArrayList implements IList {
public int[] elem;
public int usedSize;
//調(diào)用構(gòu)造方法,初始化順序表長度
public MyArrayList() {
this.elem = new int[2];
}
//判斷順序表滿不滿
public boolean isFull() {
return elem.length == usedSize;
}
//添加一個元素
@Override
public void add(int data) {
if (isFull()) {
elem = Arrays.copyOf(elem, 2 * elem.length);
}
this.elem[usedSize] = data;
this.usedSize++;
}
//該方法來 判斷 添加元素時 pos是否合法
private void checkPosOfAdd(int pos) throws PosNotLegalException {
if (pos < 0 || pos > usedSize) {
throw new PosNotLegalException("pos位置不合法!");
}
}
// 在 pos 下標(biāo)位置新增元素
@Override
public void add(int pos, int data) {
//判斷是不是正確引用
try {
checkPosOfAdd(pos);
} catch (PosNotLegalException e) {
e.printStackTrace();
}
if (isFull()) {
//擴容
elem = Arrays.copyOf(elem, 2 * elem.length);
}
//移動元素
for (int i = usedSize - 1; i >= pos; i--) {
elem[i + 1] = elem[i];
}
//插入元素
this.elem[pos] = data;
this.usedSize++;
}
//判斷順序表是否有改元素
@Override
public boolean contains(int toFind) {
for (int i = 0; i < usedSize; i++) {
if (this.elem[i] == toFind) {
return true;
}
}
return false;
}
//查找某個元素對應(yīng)的下標(biāo)位置
@Override
public int indexOf(int toFind) {
for (int i = 0; i < usedSize; i++) {
if (this.elem[i] == toFind) {
return i;
}
}
return -1;
}
//判斷pos位置是否合法
private void checkPosOfGetAndSet(int pos) throws PosNotLegalException{
if(pos < 0 || pos >= usedSize) {
throw new PosNotLegalException("get/set獲取元素的時候" +
"pos位置不合法!");
}
}
// 獲取 pos 位置的元素
@Override
public int get(int pos) {
//判斷pos位置是否合法
try {
checkPosOfGetAndSet(pos);
}catch (ClassCastException e){
e.printStackTrace();
}
return this.elem[pos];
}
// 給 pos 位置的元素設(shè)為 value -> 更新
@Override
public void set(int pos, int value) {
//判斷pos位置是否合法
try {
checkPosOfGetAndSet(pos);
}catch (ClassCastException e){
e.printStackTrace();
}
this.elem[pos] = value;
}
//刪除第一次出現(xiàn)的關(guān)鍵字key
@Override
public void remove(int toRemove) {
//1、要查找是否存在要刪除的關(guān)鍵字 toRemove
int pos = indexOf(toRemove);
if(pos == -1) {
System.out.println("沒有要刪除的數(shù)字!");
return;
}
for (int i = pos; i < usedSize-1; i++) {
elem[i] = elem[i+1];
}
usedSize--;
}
//返回數(shù)據(jù)長度
@Override
public int size() {
return this.usedSize;
}
//釋放順序表
@Override
public void clear() {
this.elem = null;
this.usedSize = 0;
}
//打印順序表
@Override
public void display() {
System.out.print("[ ");
for (int i = 0; i < usedSize; i++) {
System.out.print(elem[i] + " ");
}
System.out.println("]");
}
}
src/arrayList/PosNotLegalException類
package arrayList;
public class PosNotLegalException extends RuntimeException{
//不帶參數(shù)的構(gòu)造方法
public PosNotLegalException() {
}
//帶參數(shù)的構(gòu)造方法
public PosNotLegalException(String msg) {
super(msg);
}
}
src/arrayList/Test測試類
package arrayList;
public class Test {
public static void main(String[] args) {
MyArrayList list = new MyArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
// list.add(1,24);
// boolean temp = list.contains(24);
// if(temp){
// System.out.println("有");
// }else{
// System.out.println("沒有");
// }
// 打印鏈表
list.display();
// int is =list.indexOf(2);
// if (is >= 0) {
// System.out.println("有,在下標(biāo):" + is);
// } else {
// System.out.println("沒有");
// }
// 獲取 pos 位置的元素
// int a = list.get(3);
// System.out.println(a);
// list.set(2, 6);
// System.out.println(a);
list.remove(3);
// 打印鏈表
list.display();
}
}
今天就到這里了,感謝觀看。文章來源地址http://www.zghlxwxcb.cn/news/detail-861774.html
到了這里,關(guān)于JAVASE->數(shù)據(jù)結(jié)構(gòu)|順序表底層邏輯的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!