主要目的:
解決ArrayList 類不能改變大小的問題,主要實現數組列表動態調整大小。
1、數組類型如何選擇?由于我們不清楚數組中具體存入什么類型的數據, 我們可以聲明一個對象Object [ ] ,這樣,數組列表就可以存儲任何類型的數據了。
2、泛型<> :如果定義的一個類或接口有一個或多個類型變量,則可以使用泛型。
ArrayList<String>本身就是泛型,各種類型的變量都可以組裝成對應的List,而不必針對每個類型分別實現一個構建ArrayList的類。
泛型字母所代表含義:
E表示集合的元素類型,
K 和 V分別表示表的關鍵字與值的類型 *
T(需要時還可以用臨近的字母 U 和 S)表示“任意類型”
3、實現功能:我們主要實現arraylist的基本的增,刪,改,等功能。
核心思路:主要根據所需求大小進行調整,需要創建一個新的數組,將老數組值賦予新數組再進行詳細的變動。
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
102
|
package com.customArray0905; public class CustomArraryList<E> { Object[] data; int Size; public int getSize() { return Size; } //返回數組下標為index的元素的值 public E get( int index) { if (index< 0 || index>=Size) { throw new IndexOutOfBoundsException(); //否則return null } return (E) data[index]; } //自定義更改下標為index的元素值的方法 public void set( int index, E e) { if (index< 0 || index>=Size) { throw new IndexOutOfBoundsException(); //否則return null } data[index] = e; } public void add(E e) { ///創建新對象 容量擴大一個 Object[] newdata = new Object[Size + 1 ]; //將array中的元素重新存入更新容量后的newArray數組中去 for ( int i = 0 ; i < Size; i++) { newdata[i] = data[i]; } data = newdata; data[Size++] = e; } //自定義移除下標為index的元素的方法 public void remove( int index) { ///創建新對象 容量減少一個 Object[] newdata = new Object[Size - 1 ]; int j = 0 ; //判斷index大小是否合適存在數組中 if (index< 0 || index>=Size) { throw new IndexOutOfBoundsException(); //否則return null } //得到老對象里下標之前的所有元素并存入新對象 for ( int i = 0 ; i < index; i++) { newdata[j] = data[i]; j++; } //得到老對象里下標之后的所有元素并存入新對象 for ( int i = index + 1 ; i < Size; i++) { newdata[j] = data[i]; j++; } data = newdata; Size--; } //清除array中所有的元素 public void clear() { for ( int i = 0 ;i<Size;i++) { data[i] = null ; } Size = 0 ; } public static void main(String[] args) { CustomArraryList<String> myList = new CustomArraryList<>(); //Add System.out.println( "測試1,ADD方法" ); myList.add( "1" ); myList.add( "2" ); myList.add( "3" ); myList.add( "4" ); myList.add( "5" ); for ( int i = 0 ; i < myList.getSize(); i++) { System.out.println(myList.get(i)); } //Remove,Set myList.remove( 2 ); myList.set( 3 , "7" ); System.out.println( "測試2,移除index=2的數據,并設置index=3的數據值為7," ); for ( int i = 0 ; i < myList.getSize(); i++) { System.out.println(myList.get(i)); } //Clear myList.clear(); myList.add( "1" ); for ( int i = 0 ; i < myList.getSize(); i++) { System.out.println( "測試3,clear方法,僅剩下新添加數據 " +myList.get(i)); } //拋出錯誤 System.out.println( "測試4,拋出set錯誤" ); myList.set( 2 , "2" ); } } |
測試結果:
補充知識:java Arrays快速打印數組的數據元素列表
1、Arrays.toString
用來快速打印一維數組的數據元素列表
2、Arrays.deepToString 快速打印一個二維數組的數據元素列表
1
2
3
4
5
6
7
8
9
10
11
12
|
public static strictfp void main(String[] args) { String[][] arr = {{ "aaa" , "bbb" },{ "ccc" }}; for ( int x= 0 ;x<arr.length;x++){ for ( int y= 0 ;y<arr[x].length;y++){ System.out.println(arr[x][y]); } } //Arrays.deepToString 快速打印一個二維數組的數據元素列表 System.out.println(Arrays.deepToString(arr)); } |
以上這篇Java自定義數組列表的實現操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_49857492/article/details/108513330