1、一维数组

  • 声明

    1. //声明1
    2. int[] a = new int[3];
    3. a[0] = 3; a[1] = 4; a[2]=5;
    4. //声明2
    5. int[] b = new int[]{3,4,5};
    6. //声明3
    7. int[] c = {3,4,5};
    8. //遍历输出
    9. for(int i = 0 ; i < c.length ; i++){
    10. System.out.println(c[i]);
    11. }
  • 数组如果仅仅是声明,没有赋值,但是会有个默认值

    1. //声明1,默认[0,0,0]
    2. int[] a = new int[3];
    3. for(int i = 0 ; i < a.length ; i++){
    4. System.out.println(a[i]);
    5. }
    6. // [null,null]
    7. String[] b = new String[2];
    8. for(int i = 0 ; i < b.length ; i++){
    9. System.out.println(b[i]);
    10. }
  • 数组越界异常

    1. // [null,null]
    2. String[] a = new String[2];
    3. // null
    4. System.out.println(a[0]);
    5. // null
    6. System.out.println(a[1]);
    7. // java.lang.ArrayIndexOutOfBoundsException
    8. System.out.println(a[2]);

    2、二维数组

    1. int[][] score = new int[][]{
    2. {1,2,3},
    3. {4,5,6,8},
    4. {7,8,9}};
    5. for(int i = 0 ; i < score.length ; i++){
    6. for(int j = 0; j < score[i].length ; j++){
    7. System.out.print(score[i][j]+" ");
    8. }
    9. System.out.println();
    10. }