Java (數組的遍歷,for循環的使用)


/*多行注釋的快捷鍵:Ctrl+shift+/
快速格式化代碼快捷鍵:Ctrl+shift+f
自動導入一個包:Ctrl+shift+o
*/
package test_1;

public class Day_2 {
	
	public static void main(String args[]) {
		//一個九九乘法表的實現
		int c = 0;
		for (int a = 1; a <= 9; a++) {
			for (int b = 1; b <= a; b++) {
				c = a * b;
				System.out.printf("%d*%d=%d  ",b,a,c);
			}
			System.out.println();
		}
		System.out.println("-------------------我是華麗的分割線--------------------------");

		// 遍歷數組的方法
		// 方法1:
		int arr[] = new int[3];
		for (int a = 0; a < arr.length; a++) {
			System.out.println(arr[a]);
		}

		System.out.println("-------------------我是華麗的分割線--------------------------");
		//方法2:
		for (int a : arr) {
			System.out.println(a);
		}
		
		System.out.println("-------------------我是華麗的分割線--------------------------");
		//二維數組的遍歷
		
		int arr2[][]=new int[][]{
			{1,2,3},
			{4,5,6},
			{7,8,9}
			
		};
		
		for(int i=0;i<arr2.length;i++){
			for(int j=0;j<arr2[0].length;j++){
				System.out.print(arr2[i][j]+" ");				
			}
			System.out.println();
		}
		
	}

}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM