在本文中,選擇陣列元素,透過使用其索引值來列印元素。數組是java中將相似類型的項目儲存在一起的常用方法。可以輕鬆列印各個值以及數組的所有元素。對於列印,對於數組的所有元素,通常使用“for 循環”,它選擇從 0 到數組長度的索引。
int [] array1 = {11,22,32,42,-52,62,-72,82,-92,210}; int [][] array2 = {{11,222},{23,42},{-25,26},{-27,28},{-29,120}}; int [][][] array3 = {{{1111, -22222},{5533, 433}},{{44533, -635533},{-777733, 84433}},{{90033, 84433},{-999933, 433}}};
String[] strarray = new String[]{"One", "Two", "Three"}; String[][] strarray2 = new String[][]{{"One1", "Two2"}, {"Three3", "Four"}}; String[][][] strarray3={{{"One33", "two33"},{"three33", "433"}},{{"44533", "635533"},{"seven33", "84433"}},{{"seven33", "84433"},{"three33", "433"}}};
第 1 步 - 宣告類型並定義陣列。
步驟 2 - 根據陣列類型指定元素。這些元素也可以由使用者輸入。
第 3 步 - 從索引 0 元素開始。列印出來。
第 4 步 - 將索引增加 1 並列印下一個元素。
第 5 步 - 轉到第 4 步並繼續執行相同操作,直到列印陣列的最後一個元素。
第 6 步 - 對於 2D 數組,使用兩個單獨的循環分別控制行索引和列索引。
步驟 7 - 對於 N 維數組,使用 N 個循環分別控制 N 個索引。
我們使用不同類型分別提供了解決方案範例。
透過使用 Int 類型陣列
透過使用字串類型陣列
讓我們一一看看該程式及其輸出。
for (int n=0; n<array1.length; n++){ System.out.println(array1[n]); } ;
for (int n = 0; n < 2; n++) { for (int m=0; m< 2; m++) { System.out.print(array2[n][m] + " "); } System.out.println(); }
System.out.println("\nThe 3D Int array is:\n "); for (int n = 0; n < 3; n++) for (int m=0; m< 2; m++) for (int t = 0; t < 2; t++) System.out.println("array3[" + n + "][" + m + "][" + t + "] = " + array3[n][m][t]); };
public class newarr_multidim { public static void main(String[] args) { int [] array1 = {11,22,32,42,-52,62,-72,82,-92,210}; int [][] array2 = {{11,222},{23,42},{-25,26},{-27,28},{-29,120}}; int [][][] array3 = {{{1111, -22222},{5533, 433}},{{44533, -635533},{-777733, 84433}},{{90033, 84433},{-999933, 433}}}; //printing individual elements by index value System.out.println(array1[1]+ "\n\n" +array2[0][1] + "\n\n" + array3[1][0][1]); //printing all elements System.out.println("\nThe elements in the 1D int array are:\n"); for (int n=0; n<array1.length; n++){ System.out.println(array1[n]); } ; System.out.println("\nThe 2D Int array is:\n "); for (int n = 0; n < 2; n++) { for (int m=0; m< 2; m++) { System.out.print(array2[n][m] + " "); } System.out.println(); } System.out.println("\nThe 3D Int array is:\n "); for (int n = 0; n < 3; n++) for (int m=0; m< 2; m++) for (int t = 0; t < 2; t++) System.out.println("array3[" + n + "][" + m + "][" + t + "] = " + array3[n][m][t]); }; }
22 222 -635533 The elements in the 1D int array are: 11 22 32 42 -52 62 -72 82 -92 210 The 2D Int array is: 11 222 23 42 The 3D Int array is: array3[0][0][0] = 1111 array3[0][0][1] = -22222 array3[0][1][0] = 5533 array3[0][1][1] = 433 array3[1][0][0] = 44533 array3[1][0][1] = -635533 array3[1][1][0] = -777733 array3[1][1][1] = 84433 array3[2][0][0] = 90033 array3[2][0][1] = 84433 array3[2][1][0] = -999933 array3[2][1][1] = 433
for (int n=0; n<strarray.length; n++){ System.out.println(strarray[n]); } ;
for (int n = 0; n < 2; n++) { for (int m=0; m< 2; m++) { System.out.print(strarray2[n][m] + " "); } System.out.println(); }
System.out.println("\nThe 3D String array is:\n "); for (int n = 0; n < 3; n++) for (int m=0; m< 2; m++) for (int t = 0; t < 2; t++) System.out.println("strarray3[" + n + "][" + m + "][" + t + "] = " + strarray3[n][m][t]); };
public class newarr_multidim2 { public static void main(String[] args) { String[] strarray = new String[]{"One", "Two", "Three"}; String[][] strarray2 = new String[][]{{"One1", "Two2"}, {"Three3", "Four"}}; String[][][] strarray3={{{"One33", "two33"},{"three33", "433"}},{{"44533", "635533"},{"seven33", "84433"}},{{"seven33", "84433"},{"three33", "433"}}}; //printing some elements by index value System.out.println(strarray[1]+ "\n\n" +strarray2[0][1] + "\n\n" + strarray3[1][0][1]); //printing all elements System.out.println("\nThe 1D String array is:\n "); for (int n=0; n < strarray.length; n++){ System.out.println(strarray[n]); } ; System.out.println("\nThe 2D String array is:\n "); for (int n = 0; n < 2; n++) { for (int m=0; m< 2; m++) { System.out.print(strarray2[n][m] + " "); } System.out.println(); } System.out.println("\nThe 3D String array is:\n "); for (int n = 0; n < 3; n++) for (int m=0; m< 2; m++) for (int t = 0; t < 2; t++) System.out.println("strarray3[" + n + "][" + m + "][" + t + "] = " + strarray3[n][m][t]); }; }
Two Two2 635533 The 1D String array is: One Two Three The 2D String array is: One1 Two2 Three3 Four The 3D String array is: strarray3[0][0][0] = One33 strarray3[0][0][1] = two33 strarray3[0][1][0] = three33 strarray3[0][1][1] = 433 strarray3[1][0][0] = 44533 strarray3[1][0][1] = 635533 strarray3[1][1][0] = seven33 strarray3[1][1][1] = 84433 strarray3[2][0][0] = seven33 strarray3[2][0][1] = 84433 strarray3[2][1][0] = three33 strarray3[2][1][1] = 433
在上面的文章中,以Int和String類型為例,使用Java語言列印陣列元素。這些實例包括一維數組元素列印、2D數組元素列印和3D數組元素列印。這些元素列印方法可擴展到 N 維數組。
以上是Java程式列印數組元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!