Java 示例 - 数组输出
-
问题描述
如何将字符串数组写入输出控制台? -
解决方案
以下示例演示了通过循环将数组元素写入输出控制台。public class Welcome { public static void main(String[] args) { String[] greeting = new String[3]; greeting[0] = "This is the greeting"; greeting[1] = "for all the readers from"; greeting[2] = "Java Source ."; for (int i = 0; i < greeting.length; i++){ System.out.println(greeting[i]); } } }
-
结果
上面的代码示例将产生以下结果。This is the greeting For all the readers From Java source .
以下示例演示将数组的元素写入输出控制台import java.util.Arrays; public class HelloWorld { public static void main(String[] args) { String[] arr = new String[] {"Tutorials", "Point", ".com"}; System.out.println(Arrays.toString(arr)); } }
上面的代码示例将产生以下结果。[Tutorials, Point, .com] .
-
嵌套数组
以下示例演示了通过循环将数组元素写入输出控制台。import java.util.Arrays; public class HelloWorld { public static void main(String[] args) { String[][] deepArr = new String[][] {{"Sai", "Gopal"}, {"Pallavi", "Priya"}}; System.out.println(Arrays.toString(deepArr)); System.out.println(Arrays.deepToString(deepArr)); } }
上面的代码示例将产生以下结果。[[Sai, Gopal], [Pallavi, Priya]]