首頁 / CompScience / Programming / Languages / java / 正確使用java array
:idea:即日起到2016/04/06止,請到http://blogawards.tw/competitor?co=160幫工作達人按讚󾮗,之後到http://goo.gl/forms/SK3WNR5RlA填單,我們會有隱藏版獎品贈送喔~

正確使用java array

今天在看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:\source\\Q28.:8: ‘;’ expected
int [] k = new int[5] {1,2,3,4,5};
^
D:\source\java\Q28.java:8: not a statement
int [] k = new int[5] {1,2,3,4,5};
^
D:\source\java\Q28.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:\source\java\Q28.java:13: illegal start of type
public[] int a;
^
D:\source\java\Q28.java:13: ‘;’ expected
public[] int a;
^
*/
public[] int a;//line 13

/*

D:\source\java\Q28.java:14: ‘]’ expected
private int a[3];
^
D:\source\java\Q28.java:14: illegal start of type
private int a[3];
^
D:\source\java\Q28.java:14: <identifier> expected
private int a[3];
^
D:\source\java\Q28.java:14: ‘;’ expected
private int a[3];
^

*/

private int a[3];//line 14

/*

D:\source\java\Q28.java:15: ‘]’ expected
private int[ 3] a[];
^
D:\source\java\Q28.java:15: ‘;’ expected
private int[ 3] a[];
^

D:\source\java\Q28.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:\source\java\Q28.java:22: <identifier> expected
int ia[][] = {4,5,6},{1,2,3};
^
D:\source\java\Q28.java:22: illegal start of type
int ia[][] = {4,5,6},{1,2,3};
^
D:\source\java\Q28.java:22: <identifier> expected
int ia[][] = {4,5,6},{1,2,3};
^
D:\source\java\Q28.java:22: ‘;’ expected
int ia[][] = {4,5,6},{1,2,3};
^
D:\source\java\Q28.java:22: illegal start of type
int ia[][] = {4,5,6},{1,2,3};
^
D:\source\java\Q28.java:22: <identifier> expected
int ia[][] = {4,5,6},{1,2,3};
^
D:\source\java\Q28.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:\source\java\Q28.java:25: <identifier> expected
int[][] scores = {2,7,6},{9,3,45};
^
D:\source\java\Q28.java:25: illegal start of type
int[][] scores = {2,7,6},{9,3,45};
^
D:\source\java\Q28.java:25: <identifier> expected
int[][] scores = {2,7,6},{9,3,45};
^
D:\source\java\Q28.java:25: ‘;’ expected
int[][] scores = {2,7,6},{9,3,45};
^
D:\source\java\Q28.java:25: illegal start of type
int[][] scores = {2,7,6},{9,3,45};
^
D:\source\java\Q28.java:25: <identifier> expected
int[][] scores = {2,7,6},{9,3,45};
^
D:\source\java\Q28.java:25: ‘;’ expected
int[][] scores = {2,7,6},{9,3,45};
^

*/
int[][] scores = {2,7,6},{9,3,45};
String cats[] = {“Fluffy”,”Spot”,”Zeus”};

/*

D:\source\java\Q28.java:27: ‘;’ expected
boolean results[] = new boolean [ 3] {true,false,true};
^
D:\source\java\Q28.java:27: not a statement
boolean results[] = new boolean [ 3] {true,false,true};
^
D:\source\java\Q28.java:27: ‘;’ expected
boolean results[] = new boolean [ 3] {true,false,true};
^

*/
boolean results[] = new boolean [ 3] {true,false,true};
Integer results[] = {new Integer(3),new Integer(5),new Integer(8)};
String[] dogs = new String[]{new String(“Fido”),new String(“Spike”),new String(“Aiko”)};
}

對了,你安裝了Java沒,在Ubuntu下安裝可以參考sudo apt-get install sun-java5-jdk

馬上成為工作達人的Fans

About ㄚ琪

工作達人Fun Taiwan的創辦者及總編,可以在這裡更認識他。

One comment

  1. 謝謝這些教學分享,非常好,都是大家所需要的,太實用了……

發表迴響

你的電子郵件位址並不會被公開。 Required fields are marked *

*

Scroll To Top