Java基礎知識之一維數組的寫法
阿新 • • 發佈:2019-05-06
ray nbsp main brush () nds n) 標準 cep
public class Num { public static void main(String[] args) { arraysTest(); } /** * 一維數組的三種寫法 */ public static void arraysTest() { //第一種寫法/一維數組的標準格式 int[] m = new int[]{1, 2, 3, 4, 5}; String[] str = new String[]{"我", "是", "誰"}; //通過foreach取出數組裏的值 for (String st : str) { System.out.println(st); } //第二種寫法/簡寫格式 int[] n = {1, 2, 3, 4, 5}; System.out.println(n); //第三種寫法,初始化容量 int[] i = new int[2]; i[0] = 1; i[1] = 2; //初始化容量一旦超過初始化容量,會報java.lang.ArrayIndexOutOfBoundsException異常 //i[2]=3; System.out.println(i); } 分類: JAVA
Java基礎知識之一維數組的寫法