今天在看Java認證SCJP 5.0–猛虎出閘陣列的部份時,突然覺得不知從哪裡可以找到java array 正確使用的語法,於是想說從一些範例及SCJP的模擬試題中看是否可以找出端倪:
※猛虎出閘的最新版本為猛虎出柙雙劍合璧版:最新 OCA / OCP Java SE 7 Programmer 專業認證
在SCJP TestKing 310-035,這份試題的前面72題中發現有4個題目,是跟陣列有關的:
QUESTION NO: 2
Which two cause a compiler error? (Choose two)
A. float[] = new float(3);
B. float f2[] = new float[];
C. float[] f1 = new float[3];
D. float f3[] = new float[3];
E. float f5[] = { 1.0f, 2.0f, 2.0f };
F. float f4[] = new float[] { 1.0f. 2.0f. 3.0f};
Answer: A, B
The F. statement is incorrect. The float numbers should be separated with commas and not dots.
QUESTION NO: 28
Which three form part of correct array declarations? (Choose three)
A. public int a []
B. static int [] a
C. public [] int a
D. private int a [3]
E. private int [3] a []
F. public final int [] a
Answer: A, B, F
QUESTION NO: 51
Which two create an instance of an array? (Choose two)
A. int[] ia = new int[15];
B. float fa = new float[20];
C. char[] ca = “Some String”;
D. Object oa = new float[20];
E. int ia[][] = { 4, 5, 6, }, { 1, 2, 3 };
Answer: A, D
QUESTION NO: 62
Which two cause a compiler error? (Choose two)
A. int[] scores = {3, 5, 7};
B. int [][] scores = {2,7,6}, {9,3,45};
C. String cats[] = {“Fluffy”, “Spot”, “Zeus”};
D. boolean results[] = new boolean [3] {true, false, true};
E. Integer results[] = {new Integer(3), new Integer(5), new Integer(8)};
F. String[] dogs = new String[]{new String(“Fido”),new String(“Spike”), new String(“Aiko”)};
Answer: B, D
我把這些敘述寫在程式裡頭,並且拿來編譯,把編譯後的錯誤訊息註解起來,這樣就可以加強我們對array的了解:
public class Q28 { int i[]; int [] i; int []i; int[] i = new int [ ] {1,2,3,4,5}; int [] j = {1,2,3,4,5}; /* D:sourcejavaQ28.java:8: ';' expected int [] k = new int[5] {1,2,3,4,5}; ^ D:sourcejavaQ28.java:8: not a statement int [] k = new int[5] {1,2,3,4,5}; ^ D:sourcejavaQ28.java:8: ';' expected int [] k = new int[5] {1,2,3,4,5}; ^ */ int [] k = new int[5] {1,2,3,4,5};//line 8 Object o = new int[10]; public int a[]; static int [] a; /* D:sourcejavaQ28.java:13: illegal start of type public[] int a; ^ D:sourcejavaQ28.java:13: ';' expected public[] int a; ^ */ public[] int a;//line 13 /* D:sourcejavaQ28.java:14: ']' expected private int a[3]; ^ D:sourcejavaQ28.java:14: illegal start of type private int a[3]; ^ D:sourcejavaQ28.java:14: <identifier> expected private int a[3]; ^ D:sourcejavaQ28.java:14: ';' expected private int a[3]; ^ */ private int a[3];//line 14 /* D:sourcejavaQ28.java:15: ']' expected private int[ 3] a[]; ^ D:sourcejavaQ28.java:15: ';' expected private int[ 3] a[]; ^ D:sourcejavaQ28.java:15: <identifier> expected private int[ 3] a[]; ^ */ private int[ 3] a[];//line 15 public final int [] a; int[] ia = new int[ 15]; float fa = new float[ 20]; char[] ca = "Some String"; Object oa = new float[20]; /* D:sourcejavaQ28.java:22: <identifier> expected int ia[][] = {4,5,6},{1,2,3}; ^ D:sourcejavaQ28.java:22: illegal start of type int ia[][] = {4,5,6},{1,2,3}; ^ D:sourcejavaQ28.java:22: <identifier> expected int ia[][] = {4,5,6},{1,2,3}; ^ D:sourcejavaQ28.java:22: ';' expected int ia[][] = {4,5,6},{1,2,3}; ^ D:sourcejavaQ28.java:22: illegal start of type int ia[][] = {4,5,6},{1,2,3}; ^ D:sourcejavaQ28.java:22: <identifier> expected int ia[][] = {4,5,6},{1,2,3}; ^ D:sourcejavaQ28.java:22: ';' expected int ia[][] = {4,5,6},{1,2,3}; ^ */ int ia[][] = {4,5,6},{1,2,3}; int[] scores = {3,5,7}; /* D:sourcejavaQ28.java:25: <identifier> expected int[][] scores = {2,7,6},{9,3,45}; ^ D:sourcejavaQ28.java:25: illegal start of type int[][] scores = {2,7,6},{9,3,45}; ^ D:sourcejavaQ28.java:25: <identifier> expected int[][] scores = {2,7,6},{9,3,45}; ^ D:sourcejavaQ28.java:25: ';' expected int[][] scores = {2,7,6},{9,3,45}; ^ D:sourcejavaQ28.java:25: illegal start of type int[][] scores = {2,7,6},{9,3,45}; ^ D:sourcejavaQ28.java:25: <identifier> expected int[][] scores = {2,7,6},{9,3,45}; ^ D:sourcejavaQ28.java:25: ';' expected int[][] scores = {2,7,6},{9,3,45}; ^
對了,你安裝了Java沒,在Ubuntu下安裝可以參考sudo apt-get install sun-java5-jdk。
- 正確使用java array
- 【Java】遞增和遞減一元運算子
謝謝這些教學分享,非常好,都是大家所需要的,太實用了……