1. 程式人生 > 其它 >JAVA基礎 | 遍歷輸出二維陣列的兩種方式(210110)

JAVA基礎 | 遍歷輸出二維陣列的兩種方式(210110)

技術標籤:java

JAVA基礎 | 遍歷輸出二維陣列的兩種方式

【1】普通 for 型遍歷輸出

public class ExampleOne {
	public static void main(String[] args) {
		int a[][]= {{1,2,3},{4,5},{6,7,8,9}};
		//普通for型 遍歷輸出二維陣列
		for(int i=0;i<=a.length-1;i++) {
			for(int j=0;j<=a[i].length-1;j++) {
				System.out.print(a[i][j]+"  "
); } System.out.println(); } } }

【2】for-each 型遍歷輸出

public class ExampleTwo {
	public static void main(String[] args) {
		int a[][]= {{1,2,3},{4,5},{6,7,8,9}};
		//for-each型 遍歷輸出二維陣列
		for(int i[]:a) {
			for(int j:i) {
				System.out.print(j+"  ");
			}
			System.out.println();
		}
} }