前言
其實線性表在生活中和棧的結構差不多。昨天總結了一篇單鏈表,也是線性表的一種。
今天用另一種寫法來控制指針的移動實現數據的順序存儲結構。
需求分析
首先要明確,這種順序存儲結構的線性表底層用什么。根據之前查看過的源碼來看,list一般都是以數組為底層。我們也不例外。
其次,我們還得去定義好線性表的長度,以及每個元素的指針。
1
2
3
4
|
private Object[] arr; // 底層的結構 private int index = - 1 ; // 代表元素的索引位置 private int size; // 當前線性表的長度 private int LinearListLength = 4 ; // 線性表的默認長度 |
我們這兒只演示添加、刪除、獲取指定位置、獲取全部以及判斷是否為空這五種形式。
編碼
add方法
add方法為向線性表中添加元素,需要傳入一個泛型參數。實現思路是讓index+1然后把index賦值給數組得到索引區(qū)域,再讓size+1
總體設計比較簡單,看代碼。
1
2
3
4
5
6
7
8
9
10
11
|
public E add(E item) { // 先初始化線性表 capacity(); // 初始化完成后先把index指針后移一位,也就是+1 // 后移一位之后將要添加的元素賦值到數組中 this .arr[++index] = item; System.out.println(index); // 添加完成后長度+1 this .size++; return item; } |
getIndex方法
getIndex方法主要是用來獲取指定位置的元素,這個就很簡單了,因為底層是數組,所以我們可以直接用數組的索引去獲取。
1
2
3
|
public E getIndex( int index) { return (E) this .arr[index]; } |
pop方法
pop方法作用是刪除指定位置的元素。需要傳入一個int類型的索引。由于特殊性,我們必須得借用上面的獲取指定位置的元素的方法來實現這一步驟。
在元素刪除后,通過遍歷循環(huán)去將index位置向前移動一位。具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 刪除指定位置的元素 */ public E pop( int index) { E e = getIndex(index); if (e != null ) { for ( int i = index; i < size; i++) { arr[i] = arr[i + 1 ]; } this .size--; return e; } else { return null ; } } |
insert方法
insert方法需要傳入兩個參數,一個int類型的索引值,一個泛型數據。在指定位置插入該泛型值,然后將后面的值全部后移一位。
1
2
3
4
5
6
7
8
9
|
public E insert( int index, E item) { System.out.println(size); for ( int i = index; i < size; i++) { arr[i + 1 ] = arr[i]; } arr[index] = item; this .size++; return item; } |
getAll
這個方法不用我多說了,一個簡單的遍歷循環(huán)
1
2
3
4
5
|
public void getAll() { for (Object o : this .arr) { System.out.println(o); } } |
這兒遍歷的Object類型會自動轉化成添加元素時的類型
全部代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package com.zxy.xianxingbiao; /** * @Author Zxy * @Date 2021/2/4 16:54 * @Version 1.0 */ import java.util.Arrays; /** * 演示線性表的使用 底層使用數組 */ public class MyLinearList<E> { private Object[] arr; // 底層的結構 private int index = - 1 ; // 代表元素的索引位置 private int size; // 當前線性表的長度 private int LinearListLength = 4 ; // 線性表的默認長度 /** * 判斷線性表是否為空 */ public boolean empty() { return this .size == 0 ? true : false ; } /** * 給線性表中添加元素 */ public E add(E item) { // 先初始化線性表 capacity(); // 初始化完成后先把index指針后移一位,也就是+1 // 后移一位之后將要添加的元素賦值到數組中 this .arr[++index] = item; System.out.println(index); // 添加完成后長度+1 this .size++; return item; } /** * 在指定位置插入元素 */ public E insert( int index, E item) { System.out.println(size); for ( int i = index; i < size; i++) { arr[i + 1 ] = arr[i]; } arr[index] = item; this .size++; return item; } /** * 獲取指定位置的元素 */ public E getIndex( int index) { return (E) this .arr[index]; } /** * 刪除指定位置的元素 */ public E pop( int index) { E e = getIndex(index); if (e != null ) { for ( int i = index; i < size; i++) { arr[i] = arr[i + 1 ]; } this .size--; return e; } else { return null ; } } /** * 獲取全部的元素 */ public void getAll() { for (Object o : this .arr) { System.out.println(o); } } /** * 數組初始化或者以1.5倍容量對數組擴容 */ private void capacity() { // 數組初始化 if ( this .arr == null ) { this .arr = new Object[ this .LinearListLength]; } // 以1.5倍對數組擴容 if ( this .size - ( this .LinearListLength - 1 ) >= 0 ) { // 如果當前數組的元素個數大于了當前數組的最后一個索引值 this .LinearListLength = this .LinearListLength + ( this .LinearListLength >> 1 ); // 位運算,讓長度變成原來的1/2 this .arr = Arrays.copyOf( this .arr, this .LinearListLength); // 復制一個新的數組,用新開辟的長度 } } public static void main(String[] args) { MyLinearList<String> list = new MyLinearList<>(); list.add( "a" ); list.add( "b" ); list.add( "c" ); System.out.println(list.getIndex( 1 )); list.pop( 1 ); System.out.println(list.getIndex( 1 )); list.getAll(); } } |
總結
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!
原文鏈接:https://blog.csdn.net/weixin_43581288/article/details/113658746