-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson3_4.java
More file actions
58 lines (48 loc) · 1.15 KB
/
Lesson3_4.java
File metadata and controls
58 lines (48 loc) · 1.15 KB
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
/**
*/
public class Lesson3_4{
public static void main(String[] args){
//第一種
int[] nums = {50,60,70,80,90,100};
System.out.println(nums.length);
//for
//foreach jdk1.5之後
for(int x:nums){
System.out.println(x);
}
System.out.println("--------------:");
//print(nums);
print2(50,60,70,80,90,100);
//print3(null);
//print4(nums);
}
public static void print(int[] x){
int len = x.length;
for(int i =0;i<len;i++){
System.out.println(x[i]);
}
}
//jdl1.5 可變參數
//可變參數只能是參數列表中的最後一個
//可變參數作為數組使用
public static void print2(int k,int... x){
int len = x.length;
for(int i =0;i<len;i++){
System.out.println(x[i]);
}
}
//測試陣列的異常NullPointerException(空指針)
public static void print3(int[] x){
//java.lang.NullPointerException
//當一個變量為null(沒有賦值時),我們去調用了該變量的屬性和方法
int len = x.length;
}
//測試陣列的異常,陣列下標越界
public static void print4(int[] x){
// java.lang.ArrayIndexOutOfBoundsException
//當一個變量為null(沒有賦值時),我們去調用了該變量的屬性和方法
for(int i =0;i<=x.length;i++){
System.out.println(x[i]);
}
}
}