正确使用java array

今天在看Java认证SCJP 5.0–猛虎出闸阵列的部份时,突然觉得不知从哪里可以找到java array 正确使用的语法,于是想说从一些范例及SCJP的模拟试题中看是否可以找出端倪:

※猛虎出闸的最新版本为猛虎出柙双剑合璧版:最新 OCA / OCP Java SE 7 Programmer 专业认证

[adsense]

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\java\Q28.java: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

1 則留言

  1. 谢谢这些教学分享,非常好,都是大家所需要的,太实用了……

Comments are closed.