在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]]