在本文中,选择数组元素,通过使用其索引值来打印元素。数组是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中文网其他相关文章!