從新認識基本類型的變量數組
理解數組的名與實spa
數組 = 數組變量 + 數組實體code
數組的長度blog
public class WhatIsArray { public static void main(String[] args) { int[] book = new int[10]; System.out.println(book[0]); // 訪問數組的第一位 System.out.println(book[1]); // 訪問數組的第二位 System.out.println("數組的長度是:" + book.length); // 查詢數組的長度 } }
數組索引越界和初始值索引
public class IndexOutOfBoundExample { public static void main(String[] args) { int[] array = new int[5]; System.out.println(array[array.length-1]); System.out.println(array[array.length]); // array[5] 這樣就會越界 } }
讓變量指向新的數組內存
public class AssignArray { public static void main(String[] args) { int[] book = new int[3]; book[0] = 9; System.out.println("book長度爲" + book.length + "。book[0] = " + book[0]); book = new int[32]; System.out.println("book長度爲" + book.length + "。book[0] = " + book[0]); } }