Java Two Dimensional array example(二維陣列程式範例)
使用相關:
一維陣列 one Dimensional
二維陣列 two Dimensional
for 、int
顯示結果:
你可以試試不要看以下的程式寫出上圖的程式來~~
當然你還是可以參考的~~
綀習~ ~綀習~ ~多多益善~~
二維陣列程式範例:
public class D2ArrTest { /** * @param args */ public static void main(String[] args) { //---init data start-------- //Basic data in number int[] number = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; //object arr int arr[][] = new int[5][5]; //number index flag int numberIndex = 0; //object arr max length int arrMax = arr.length; // number size int numberSize = number.length; //---init data end-------- //---set value start-------- for (int i = 0; i < arrMax; i++) { arr[i] = new int[arrMax]; for (int j = 0; j < arr[i].length; j++) { if (numberIndex < numberSize) { arr[i][j] = number[numberIndex]; } else { arr[i][j] = 0; } numberIndex++; }//j for end }//i for end //---set value end-------- //---print start-------- System.out.println("------顯示------"); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { int msg = arr[i][j]; if (msg < 10) { System.out.print("0"+msg); }else{ System.out.print(msg); } System.out.print(" "); }//j for end System.out.println(""); }//i for end System.out.println("---------------"); //-----print end-------- } }
其它文章
沒有留言:
張貼留言