在Java中,如何将字符串数组写入输出控制台?
以下示例演示了通过循环将数组的元素写入输出控制台。
package com.yiibai;
public class ArrayOutput {
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 .
示例-2
以下示例演示了将数组的元素写入输出控制台 -
package com.yiibai;
import java.util.*;
public class ArrayOutput2 {
public static void main(String[] args) {
String[] arr = new String[] { "http://", "www", "yiibai", ".com" };
System.out.println(Arrays.toString(arr));
}
}
执行上面示例代码,得到以下结果 -
[http://, www, yiibai, .com]
嵌套数组示例
以下示例演示了通过循环将数组的元素写入输出控制台。
package com.yiibai;
import java.util.*;
public class ArrayOutput3 {
public static void main(String[] args) {
String[][] deepArr = new String[][] { { "Max", "Min" },
{ "Sum", "Avg" } };
System.out.println(Arrays.toString(deepArr));
System.out.println(Arrays.deepToString(deepArr));
}
}
执行上面示例代码,得到以下结果 -
[[Ljava.lang.String;@152b6651, [Ljava.lang.String;@544a5ab2]
[[Max, Min], [Sum, Avg]]