JAVA二級考試已經通過了一年了,最近準備SCJP的認證考試,下載了一些資料,從最基礎的開始複習。
/**
* @author 木炭
* 數組
*/
public class Array {
/**
* @param args
*/
public static void main(String[] args) {
//聲明一個數組不需分配任何存儲空間
//int num1[5];];//錯誤,Syntax error on token "5", delete this token
//一個數組的大小將在數組使用new 關鍵字真正創建時被給定,例如:
int num2[];
num2 = new int[5];
//這個例子也可以使用一行語句完成:
int num[] = new int[5];
//下面的程序會引起一個 ArrayIndexOutOfBoundsException 異常。
/*for(int i =0; i<6; i++){
num[i]=i*2;
}*/
//訪問一個Java 數組的標準習慣用法是使用數組的length 成員
for(int i =0; i<num.length; i++){
num[i]=i*2;
System.out.println("num[i]="+num[i]);
}
//數組聲明和初始化相結合
int k[]=new int[] {0,1,2,3,4};
//注意,你沒有必要確定數組元素的數量。
//int[] k=new int[5] {0,1,2,3,4}; //Cannot define dimension expressions when an array initializer is provided
//你可以創建數組的同時確定任何數據類型
String s[]=new String[] {"Zero","One","Two","Three","Four"};
System.out.println("s[0]="+s[0]);//這句將會輸出s[0]=Zero。
//數組的默認值,整型的數組總是被置0,布爾值總是被置false
int[] ai = new int[10];
System.out.println("ai[0]="+ai[0]);
int ia1[]= {1,2,3};
int[] ia2 = {1,2,3};
int ia3[] = new int[] {1,2,3};
System.out.print("ia3.length="+ia3.length);
}
}
* @author 木炭
* 數組
*/
public class Array {
/**
* @param args
*/
public static void main(String[] args) {
//聲明一個數組不需分配任何存儲空間
//int num1[5];];//錯誤,Syntax error on token "5", delete this token
//一個數組的大小將在數組使用new 關鍵字真正創建時被給定,例如:
int num2[];
num2 = new int[5];
//這個例子也可以使用一行語句完成:
int num[] = new int[5];
//下面的程序會引起一個 ArrayIndexOutOfBoundsException 異常。
/*for(int i =0; i<6; i++){
num[i]=i*2;
}*/
//訪問一個Java 數組的標準習慣用法是使用數組的length 成員
for(int i =0; i<num.length; i++){
num[i]=i*2;
System.out.println("num[i]="+num[i]);
}
//數組聲明和初始化相結合
int k[]=new int[] {0,1,2,3,4};
//注意,你沒有必要確定數組元素的數量。
//int[] k=new int[5] {0,1,2,3,4}; //Cannot define dimension expressions when an array initializer is provided
//你可以創建數組的同時確定任何數據類型
String s[]=new String[] {"Zero","One","Two","Three","Four"};
System.out.println("s[0]="+s[0]);//這句將會輸出s[0]=Zero。
//數組的默認值,整型的數組總是被置0,布爾值總是被置false
int[] ai = new int[10];
System.out.println("ai[0]="+ai[0]);
int ia1[]= {1,2,3};
int[] ia2 = {1,2,3};
int ia3[] = new int[] {1,2,3};
System.out.print("ia3.length="+ia3.length);
}
}
問題 )怎樣通過一個語句改變數組大小同時保持原值不變?
1) Use the setSize method of the Array class
2) Use Util.setSize(int iNewSize)
3) use the size() operator
4) None of the above
答案 4)你不能改變一個數組的大小。你需要創建一個不同大小的臨時數組,然後將原數組中的內容放進去。
1) Use the setSize method of the Array class
2) Use Util.setSize(int iNewSize)
3) use the size() operator
4) None of the above
答案 4)你不能改變一個數組的大小。你需要創建一個不同大小的臨時數組,然後將原數組中的內容放進去。
問題 ) 你想用下面的代碼查找數組最後一個元素的值,當你編譯並運行它的時候,會發
生什麼?
public class MyAr{
public static void main(String args[]){
int[] i = new int[5];
System.out.println(i[5]);}
}
1) Compilation and output of 0
2) Compilation and output of null
3) Compilation and runtime Exception
4) Compile time error
答案 3) 會得到一個運行時錯誤。因爲數組從0 開始索引,並且最後一個元素是i[4]而不是i[5]。
java.lang.ArrayIndexOutOfBoundsException:
生什麼?
public class MyAr{
public static void main(String args[]){
int[] i = new int[5];
System.out.println(i[5]);}
}
1) Compilation and output of 0
2) Compilation and output of null
3) Compilation and runtime Exception
4) Compile time error
答案 3) 會得到一個運行時錯誤。因爲數組從0 開始索引,並且最後一個元素是i[4]而不是i[5]。
java.lang.ArrayIndexOutOfBoundsException:
問題 )一個多維數組的初始化,下面哪一個能工作?
1) int i =new int[3][3];
2) int[] i =new int[3][3];
3) int[][] i =new int[3][3];
4) int i[3][3]=new int[][];
答案 3) int[][] i=new int[3][3];
1) int i =new int[3][3];
2) int[] i =new int[3][3];
3) int[][] i =new int[3][3];
4) int i[3][3]=new int[][];
答案 3) int[][] i=new int[3][3];
問題 )當你試着編譯運行下面的代碼的時候,可能會發生什麼?
public class Ardec{
public static void main(String argv[]){
Ardec ad = new Ardec();
ad.amethod();}
public void amethod(){
int ia1[]= {1,2,3};
int[] ia2 = {1,2,3};
int ia3[] = new int[] {1,2,3};
System.out.print(ia3.length);}
}
1) Compile time error, ia3 is not created correctly
2) Compile time error, arrays do not have a length field
3) Compilation but no output
4) Compilation and output of 3
答案 4) Compilation and output of 3 所有的數組的聲明都是正確的。
public class Ardec{
public static void main(String argv[]){
Ardec ad = new Ardec();
ad.amethod();}
public void amethod(){
int ia1[]= {1,2,3};
int[] ia2 = {1,2,3};
int ia3[] = new int[] {1,2,3};
System.out.print(ia3.length);}
}
1) Compile time error, ia3 is not created correctly
2) Compile time error, arrays do not have a length field
3) Compilation but no output
4) Compilation and output of 3
答案 4) Compilation and output of 3 所有的數組的聲明都是正確的。