我們可能知道 js有個length函數,java也有啊length函數
例
如果數組是data[],則data.length
| 代碼如下 |
複製代碼 |
byte[] phone =new byte[81]; //建立一個byte類型的數組,長度為81 phone[i]!=0中phone[i]! //數組的第i的位置不等0 |
如:
| 代碼如下 |
複製代碼 |
byte[] phone =new byte[81]; //建立一個byte類型的數組,長度為81 phone[1]!=0中phone[1]! //數組第二個取值不等於0 |
同時給樓主舉個例子:
| 代碼如下 |
複製代碼 |
public class StudyArrary { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub byte[] phone =new byte[81]; //建立一個byte類型的數組,長度為81 for(int a = 0; a < 81; a++) phone[a]=(byte)a; //數組的第i的位置不等0 for(int i = 0; i < 81; i++){ if (phone[i]!=0) { System.out.println("phone["+i+"]不等於0"+",phone"+i+"是"+phone[i]); } else System.out.println("phone["+i+"]等於0"+",phone"+i+"是"+phone[i]); } } } |
二維數組的長度
| 代碼如下 |
複製代碼 |
public class Lesson{ public static void main(String [] args){ //二維數組的聲明方式: //資料類型 [][] 數組名稱 = new 資料類型 [長度][長度] ; //資料類型 [][] 數組名稱 = {{123},{456}} ; int [][] num = new int [3][3]; //定義了三行三列的二維數組 num[0][0] = 1; //給第一行第一個元素賦值 num[0][1] = 2; //給第一行第二個元素賦值 num[0][2] = 3; //給第一行第三個元素賦值 num[1][0] = 4; //給第二行第一個元素賦值 num[1][1] = 5; //給第二行第二個元素賦值 num[1][2] = 6; //給第二行第三個元素賦值 num[2][0] = 7; //給第三行第一個元素賦值 num[2][1] = 8; //給第三行第二個元素賦值 num[2][2] = 9; //給第三行第三個元素賦值 for(int x = 0; x for(int y = 0; y System.out.print(num[x][y]); } System.out.println("/n"); } } }
|
//數組值arr[x][y]表示指定的是第x行第y列的值。
//在使用二維數組對象時,注意length所代表的長度,
//數組名後直接加上length(如arr.length),所指的是有幾行(Row);
//指定索引後加上length(如arr[0].length),指的是該行所擁有的元素,也就是列(Column)數目