陣列與for迴圈
5.2陣列
除了前邊說的字串,數字,布林這三個基礎的資料型別外,其實還有一個基礎資料型別,那就是陣列,陣列就是一組型別相同的資料組合在一起。
比如這樣一堆數字:1,3,5,7,9,11我們用陣列來儲存這些數字
// 宣告一個 int 陣列的變數,陣列大小為6 int[] numbers = new int[6]; numbers[0] = 1; numbers[1] = 3; numbers[2] = 5; numbers[3] = 7; numbers[4] = 9; numbers[5] = 11;
同樣如果想獲取陣列的某一個數據,也是用下標,語法如下
// 得到陣列中第三個數字 intnum = numbers[2]; System.out.println(num);
長度
陣列物件都有一個值length,可以得到該陣列的長度
public static void main(String[] args) { int[] numbers = new int[8]; int size = numbers.length; System.out.println(size); }
陣列初始化後,並沒有儲存實際的值,int型別的資料預設值是0,所以如果沒有完成陣列的賦值,那麼int陣列的每一個值都是0;String型別的資料預設值是null,所以沒有賦值時,String陣列的每一個值都是null.
我們來模擬一下excel表格,這個表格存放了學生成績,第一列存放的是學生名稱,第二列存放的是學生成績
需要getScores,getNames和print方法,列印格式為姓名:成績,比如小Z:40
public class Test{ public static void main(String[] args) { String[] names = getNames(); int[] scores = getScores(); print(names, scores, 0, names.length); } // print 方法 public staticvoid print(String[] names, int[] scores, int begin, int total){ if (begin >= total) { return; } System.out.println(names[begin] + ":" + scores[begin]); // 遞增begin begin++; print(names,scores,begin, total); } public static int[] getScores() { int[] scores = new int[5]; scores[0] = 88; scores[1] = 90; scores[2] = 70; scores[3] = 56; scores[4] = 40; return scores; } public static String[] getNames() { String[] names = new String[5]; names[0] = "小D"; names[1] = "小E"; names[2] = "小F"; names[3] = "小X"; names[4] = "小Z"; return names; } }
執行結果為:
小D:88
小E:90
小F:70
小X:56
小Z:40
5.2 for迴圈
如果我們想遍歷陣列中的每一項資料就需要用到迴圈的知識
大多數情況下,我們操作表格資料都有兩個需求
- 錄入資料到計算機中
- 遍歷分析資料
首先錄入資料
String[] tables = new String[3]; tables[0] = "張三"; tables[1] = "李四"; tables[2] = "王五";
解決完儲存之後,我們就需要遍歷出陣列中的每一條記錄
for(int i = 0; i< tables.length;i++){ String name = tables[i]; System.out.println(name); }
實際上陣列的資料初始化,還有一個簡寫的方式,如果資料已經很確定的情況下,
那我們可以使用如下的語法
public static void main(String[] args) { String[] tables = new String[]{"張三","李四", "王五"}; for(int i = 0; i < tables.length; i++){ String name = tables[i]; System.out.println(i+":"+name); } }
如果是數字陣列的話,那就是
new int[]{39,40,89};
浮點資料就是
new double[]{39,40,89};
還有一種for迴圈程式碼如下
for (String name : tables) { }
這種稱為增強for迴圈,等同於
for(int i = 0; i < tables.length; i++){ String name = tables[i]; }
增強for
迴圈是JDK1.5
以後出來的一個高階for
迴圈,專門用來遍歷陣列和集合的。它的內部原理其實是個Iterator
迭代器,所以在遍歷的過程中,不能對集合中的元素進行增刪操作。
格式:
for(元素的資料型別 變數 : Collection集合or陣列){ }
它用於遍歷Collection和陣列。通常只進行遍歷元素,不要在遍歷的過程中對集合元素進行增刪操作。
練習一:遍歷陣列
int[] arr = new int[]{11,22,33};
for (int n : arr) {
//變數n代表被遍歷到的陣列元素</span>
System.out.println(n);
}
練習二:遍歷集合
Collection<String> coll = new ArrayList<String>();
coll.add("a1");
coll.add("a2");
coll.add("a3");
coll.add("a4");
for(String str : coll){
//變數Str代表被遍歷到的集合元素</span>
System.out.println(str);
}
注意:新for迴圈必須有被遍歷的目標。目標只能是Collection或者是陣列。
建議:遍歷陣列時,如果僅為遍歷,可以使用增強for如果要對陣列的元素進行 操作
巢狀迴圈
例:輸出9*9乘法表
程式碼:
public static void main(String[] args) { for (int i = 1; i <= 9; i++) { String line = ""; for (int j = 1; j <= i; j++) { // 拼接line字串,打印出 類似 1*2=2 2*2=4 這樣的效果 line = line + j + "*" + i + "=" + j * i + " "; } System.out.println(line); } }
再回到學生成績表,用for迴圈實現遍歷
1 public class Test6{ 2 3 public static void main(String[] args) { 4 String[] names = getNames(); 5 int[] scores = getScores(); 6 print(names, scores); 7 } 8 9 public static void print(String[] names, int[] scores) { 10 for (int i = 0; i < names.length; i++) { 11 12 System.out.println(names[i] + ":" + scores[i]); 13 } 14 } 15 16 17 public static int[] getScores() { 18 19 int[] scores = new int[5]; 20 21 scores[0] = 88; 22 scores[1] = 90; 23 scores[2] = 70; 24 scores[3] = 56; 25 scores[4] = 40; 26 27 return scores; 28 } 29 30 public static String[] getNames() { 31 32 String[] names = new String[5]; 33 34 names[0] = "小D"; 35 names[1] = "小E"; 36 names[2] = "小F"; 37 names[3] = "小X"; 38 names[4] = "小Z"; 39 40 return names; 41 } 42 43 }