Java数组遍历输出

时间:2022-10-02 22:35:02

在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]);
}
}
}
Java

执行上面示例代码,得到以下结果 -

This is the greeting
For all the readers From
Java source .
Shell

示例-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));
}
}
Java

执行上面示例代码,得到以下结果 -

[http://, www, yiibai, .com]
Shell

嵌套数组示例

以下示例演示了通过循环将数组的元素写入输出控制台。

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));
}
}
Java

执行上面示例代码,得到以下结果 -

[[Ljava.lang.String;@152b6651, [Ljava.lang.String;@544a5ab2]
[[Max, Min], [Sum, Avg]]