本帖最後由 tonyh 於 2016-5-27 20:40 編輯
利用length屬性, 可取得陣列的長度, 即該陣列包含了幾位成員.
int a[]={1,2,3,4};
int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4},{1,2,3,4},{1,2,3,4}}};
上列三個陣列長度分別為何呢?- public class Ch41
- {
- public static void main(String args[])
- {
- int a[]={1,2,3,4};
- int b[][]={{1,2,3,4},{1,2,3,4},{1,2,3,4}};
- int c[][][]={{{1,2,3,4},{1,2,3,4},{1,2,3,4}},{{1,2,3,4},{1,2,3,4},{1,2,3,4}}};
- System.out.println(a.length); //4
- System.out.println(b.length); //3
- System.out.println(c.length); //2
- }
- }
複製代碼 |