forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeArray.java
More file actions
39 lines (31 loc) · 778 Bytes
/
SafeArray.java
File metadata and controls
39 lines (31 loc) · 778 Bytes
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
public class SafeArray {
private int[] arr;
public SafeArray() {
this(10); // 預設 10 個元素
}
public SafeArray(int length) {
arr = new int[length];
}
public void showElement() {
for(int i : arr) {
System.out.print(i + " ");
}
}
public int getElement(int i) {
if(i >= arr.length || i < 0) {
System.err.println("索引錯誤");
return 0;
}
return arr[i];
}
public int getLength() {
return arr.length;
}
public void setElement(int i, int data) {
if(i >= arr.length || i < 0) {
System.err.println("索引錯誤");
return;
}
arr[i] = data;
}
}