自定義類數(shù)組的創(chuàng)建和初始化
剛剛在慕課學(xué)習(xí)Java的集合類List過程中,向集合中添加元素時(shí),遇到一個(gè)問題:
定義了一個(gè)Course類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Course { private String id; private String name; //課程名稱 //get set方法 public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } |
在測(cè)試類中有一個(gè)Course類的List集合allCourses,一個(gè)向該List集合添加元素的方法addToAllCourses() ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class ListTest { private List allCourses; //用于存放備選課程的List //構(gòu)造函數(shù),初始化allCourses public ListTest() { this .allCourses = new ArrayList(); } public void addToAllCourses(String id, String name) { //List的add(Object e), 添加一個(gè)元素 Course cs = new Course(); //創(chuàng)建一個(gè)Course對(duì)象,并設(shè)置其參數(shù) cs.setId(id); cs.setName(name); allCourses.add(cs); // List的addAll(Collection c)方法 Course[] courses = new Course[ 3 ]; courses[ 0 ].setId( "2" ); courses[ 0 ].setName( "C語(yǔ)言" ); courses[ 1 ].setId( "3" ); courses[ 1 ].setName( "數(shù)據(jù)庫(kù)" ); courses[ 2 ].setId( "4" ); courses[ 2 ].setName( "計(jì)算機(jī)網(wǎng)絡(luò)" ); allCourses.addAll(Arrays.asList(courses)); //在該方法中 參數(shù)必須為collection類型,因此必須用工具類進(jìn)行類型轉(zhuǎn)換 } } |
主函數(shù)測(cè)試
1
2
3
4
5
6
7
8
9
|
public static void main(String[] args) { ListTest list = new ListTest(); list.addToAllCourses( "1" , "數(shù)據(jù)結(jié)構(gòu)" ); List<Course> li = list.getAllCourses(); for ( int i = 0 ; i < li.size(); i++) { System.out.println((i + 1 ) + ": " + li.get(i).getId() + " " + li.get(i).getName()); } } |
乍看是沒有問題的,但是一運(yùn)行,問題就來了,myeclipse報(bào)出了空指針異常。異常拋出點(diǎn)為addToAllCourses()方法中 Course類數(shù)組courses在賦值時(shí)的代碼。
既然是空指針異常,也就是說,courses[0], courses[1], courses[2]都是沒有被初始化的。
一般而言,如下的數(shù)組定義在myeclipse中是不會(huì)報(bào)錯(cuò)的:
1
2
3
|
String[] s = new String[ 3 ]; s[ 0 ] = "000000" ; System.out.println(s[ 0 ]); |
但是,我的代碼中,數(shù)組的類型為自定義的類,而非Java本身提供的類(如String類),因而我懷疑是不是我的數(shù)組定義出了問題. 查閱資料后發(fā)現(xiàn),自定義類的數(shù)組定義后的初始化應(yīng)該如下:
1
2
3
4
|
Course[] courses = new Course[ 3 ]; courses[ 0 ] = new Course(); courses[ 0 ].setName( "0000000" ); System.out.println(courses[ 0 ].getName()); |
也就是說,在聲明了自定義類的數(shù)組之后,對(duì)每一個(gè)數(shù)組元素的初始化,都要為其new一個(gè)對(duì)象出來使得指針指向該對(duì)象,Java語(yǔ)言本身是不提供在自定義類數(shù)組聲明時(shí)候自動(dòng)創(chuàng)建新對(duì)象的方式的。
此處順便再補(bǔ)充一下類二維數(shù)組的定義及初始化,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*Java提供類*/ //方式一: String[][] s = new String[][] { { "a" , "b" , "c" }, { "d" , "e" , "f" } }; //方式二 int r = 0 ; String[][] s = new String[ 2 ][ 3 ]; for ( int i = 0 ; i < 2 ; i++) for ( int j = 0 ; j < 3 ; j++) { s[i][j] = String.valueOf(r++); } /*自定義類*/ Course[][] courses = new Course[ 2 ][ 3 ]; //聲明 courses[ 0 ][ 0 ] = new Course(); //使用時(shí)new一個(gè)實(shí)例 courses[ 0 ][ 0 ].setId( "0" ); courses[ 0 ][ 0 ].setName( "000000" ); System.out.println(courses[ 0 ][ 0 ].getId() + " " + courses[ 0 ][ 0 ].getName()); //測(cè)試 不報(bào)空指針異常 |
自定義類封裝數(shù)組,添加類方法實(shí)現(xiàn)數(shù)據(jù)
1、具體見注釋
2、后續(xù)或有更新
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
public class MyArray { private long [] array; private int cnt; // 自定義數(shù)組類的元素個(gè)數(shù) /** 使用自定義類封裝數(shù)組,添加類方法實(shí)現(xiàn)數(shù)據(jù)操作 */ public MyArray() { array = new long [ 50 ]; } public MyArray( int size) { array = new long [size]; } /** 插入數(shù)據(jù),返回值為空 */ public void insert( long insertValue) { array[cnt++] = insertValue; } /** 顯示數(shù)據(jù),返回值為空 */ public void display() { System.out.print( "[" ); for ( int i = 0 ; i < cnt ; ++i) { System.out.print(array[i]); if (i != cnt - 1 ) { System.out.print( "," ); } } System.out.println( "]" ); } /** 按值查找數(shù)據(jù),返回索引值 算法:線性查找 */ public int search( long targetValue) { int i; int searchResult; for (i = 0 ; i < cnt; ++i) { if (targetValue == array[i]) { break ; } } if (i == cnt) { searchResult = - 1 ; } else { searchResult = i; } return searchResult; // 保持單一出口 } /** 按索引查找數(shù)據(jù),返回值為目標(biāo)數(shù)據(jù) */ public long get( int targetIndex) { if (targetIndex < 0 || targetIndex >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { return array[targetIndex]; } } /** 按值刪除數(shù)據(jù),返回其索引值 */ public int deleteByValue( long deleteValue) { int i; int deleteResult; for (i = 0 ; i < cnt; ++i) { if (array[i] == deleteValue) { int j; for (j = i; j < cnt- 1 ; ++j) { array[j] = array[j+ 1 ]; } array[j] = array[--cnt]; break ; // 僅刪除從左到右第一個(gè)找到的目標(biāo)值 } } if (i == cnt) { deleteResult = - 1 ; } else { deleteResult = i; } return deleteResult; // 保持單一出口 } /** 按索引刪除數(shù)據(jù),返回值為空 */ public void delete( int index) { if (index < 0 || index >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { int i; for (i = index; i < cnt - 1 ; ++i) { array[i] = array[i + 1 ]; } //array[i] = array[cnt - 1]; //cnt--; array[i] = array[--cnt]; // 替換上兩行 } } /** 根據(jù)索引值,更新數(shù)據(jù),返回值為空 */ public void update( int index, int newValue) { if (index < 0 || index >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { array[index] = newValue; } } public static void main(String[] args) { MyArray array = new MyArray( 3 ); array.insert( 13 ); array.insert( 34 ); array.insert( 90 ); array.display(); array.deleteByValue( 34 ); array.display(); } } |
3、添加自定義有序數(shù)組類
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
public class MyOrderArray { private long [] array; private int cnt; // 自定義數(shù)組類的元素個(gè)數(shù) /** 使用自定義類封裝數(shù)組,添加類方法實(shí)現(xiàn)數(shù)據(jù)操作 */ public MyOrderArray() { array = new long [ 50 ]; } public MyOrderArray( int size) { array = new long [size]; } /** 按序插入數(shù)據(jù),返回值為空 */ public void insert( long insertValue) { int i; for (i = 0 ; i < cnt; ++i) { if (array[i] > insertValue) { break ; } } int j; for (j = cnt; j > i; --j) { array[j] = array[j - 1 ]; } array[i] = insertValue; cnt++; } /** 顯示數(shù)據(jù),返回值為空 */ public void display() { System.out.print( "[" ); for ( int i = 0 ; i < cnt ; ++i) { System.out.print(array[i]); if (i != cnt - 1 ) { System.out.print( "," ); } } System.out.println( "]" ); } /** 按值查找數(shù)據(jù),返回索引值 算法:線性查找 */ public int search( long targetValue) { int i; int searchResult; for (i = 0 ; i < cnt; ++i) { if (targetValue == array[i]) { break ; } } if (i == cnt) { searchResult = - 1 ; } else { searchResult = i; } return searchResult; // 保持單一出口 } /** 按索引查找數(shù)據(jù),返回值為目標(biāo)數(shù)據(jù) */ public long get( int targetIndex) { if (targetIndex < 0 || targetIndex >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { return array[targetIndex]; } } /** 按值刪除數(shù)據(jù),返回其索引值 */ public int deleteByValue( long deleteValue) { int i; int deleteResult; for (i = 0 ; i < cnt; ++i) { if (array[i] == deleteValue) { int j; for (j = i; j < cnt- 1 ; ++j) { array[j] = array[j+ 1 ]; } array[j] = array[--cnt]; break ; // 僅刪除從左到右第一個(gè)找到的目標(biāo)值 } } if (i == cnt) { deleteResult = - 1 ; } else { deleteResult = i; } return deleteResult; // 保持單一出口 } /** 按索引刪除數(shù)據(jù),返回值為空 */ public void delete( int index) { if (index < 0 || index >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { int i; for (i = index; i < cnt - 1 ; ++i) { array[i] = array[i + 1 ]; } //array[i] = array[cnt - 1]; //cnt--; array[i] = array[--cnt]; // 替換上兩行 } } /** 根據(jù)索引值,更新數(shù)據(jù),返回值為空 */ public void update( int index, int newValue) { if (index < 0 || index >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { array[index] = newValue; } } public static void main(String[] args) { MyOrderArray array = new MyOrderArray( 3 ); array.insert( 90 ); array.insert( 13 ); array.insert( 34 ); array.display(); array.deleteByValue( 34 ); array.display(); } } |
4、MyArray類與MyOrderArray類目前僅區(qū)別于insert方法,后續(xù)或有更新
5、MyOrderArray類新增二分查找方法binarySearch,具體細(xì)節(jié)見該方法代碼
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
public class MyOrderArray { private long [] array; private int cnt; // 自定義數(shù)組類的元素個(gè)數(shù) /** 使用自定義類封裝數(shù)組,添加類方法實(shí)現(xiàn)數(shù)據(jù)操作 */ public MyOrderArray() { array = new long [ 50 ]; } public MyOrderArray( int size) { array = new long [size]; } /** 按序插入數(shù)據(jù),返回值為空 */ public void insert( long insertValue) { int i; for (i = 0 ; i < cnt; ++i) { if (array[i] > insertValue) { break ; } } int j; for (j = cnt; j > i; --j) { array[j] = array[j - 1 ]; } array[i] = insertValue; cnt++; } /** 顯示數(shù)據(jù),返回值為空 */ public void display() { System.out.print( "[" ); for ( int i = 0 ; i < cnt ; ++i) { System.out.print(array[i]); if (i != cnt - 1 ) { System.out.print( "," ); } } System.out.println( "]" ); } /** 按值查找數(shù)據(jù),返回索引值 算法:線性查找 */ public int search( long targetValue) { int i; int searchResult; for (i = 0 ; i < cnt; ++i) { if (targetValue == array[i]) { break ; } } if (i == cnt) { searchResult = - 1 ; } else { searchResult = i; } return searchResult; // 保持單一出口 } /** 按值查找數(shù)據(jù),返回索引值 算法:二分查找 */ public int binarySearch( long targetValue) { int middle = 0 ; int low = 0 ; int top = cnt; while ( true ) { middle = (top + low) / 2 ; if (targetValue == array[middle]) { return middle; } else if (low > top) { return - 1 ; } else if (targetValue < array[middle]) { top = middle - 1 ; // 切記減一 } else if (targetValue >= array[middle]) { low = middle + 1 ; // 切記加一 } } } /** 按索引查找數(shù)據(jù),返回值為目標(biāo)數(shù)據(jù) */ public long get( int targetIndex) { if (targetIndex < 0 || targetIndex >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { return array[targetIndex]; } } /** 按值刪除數(shù)據(jù),返回其索引值 */ public int deleteByValue( long deleteValue) { int i; int deleteResult; for (i = 0 ; i < cnt; ++i) { if (array[i] == deleteValue) { int j; for (j = i; j < cnt- 1 ; ++j) { array[j] = array[j+ 1 ]; } array[j] = array[--cnt]; break ; // 僅刪除從左到右第一個(gè)找到的目標(biāo)值 } } if (i == cnt) { deleteResult = - 1 ; } else { deleteResult = i; } return deleteResult; // 保持單一出口 } /** 按索引刪除數(shù)據(jù),返回值為空 */ public void delete( int index) { if (index < 0 || index >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { int i; for (i = index; i < cnt - 1 ; ++i) { array[i] = array[i + 1 ]; } //array[i] = array[cnt - 1]; //cnt--; array[i] = array[--cnt]; // 替換上兩行 } } /** 根據(jù)索引值,更新數(shù)據(jù),返回值為空 */ public void update( int index, int newValue) { if (index < 0 || index >= cnt) { throw new ArrayIndexOutOfBoundsException(); } else { array[index] = newValue; } } public static void main(String[] args) { MyOrderArray array = new MyOrderArray( 3 ); array.insert( 90 ); array.insert( 13 ); array.insert( 34 ); array.display(); //array.deleteByValue(34); System.out.println(array.binarySearch( 90 )); array.display(); } } |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/Keplery_/article/details/79601653