输出 Java 数组中的字符串的方法有三种:使用 System.out.println() 直接输出。使用循环对数组中的每个字符串进行处理或以不同格式输出。使用 StringBuilder 连接数组中的字符串并以特定格式输出。
如何输出 Java 数组中的字符串
直接输出
最简单的输出数组中字符串的方法是使用 System.out.println()。例如:
String[] fruits = {"苹果", "香蕉", "橙子"};
System.out.println(Arrays.toString(fruits));使用循环
如果您希望对数组中的每个字符串进行额外的处理或以不同格式输出,则可以使用循环。例如:
String[] fruits = {"苹果", "香蕉", "橙子"};
for (String fruit : fruits) {
// 在这里对每个字符串进行操作
System.out.println(fruit);
}字符串连接
要将数组中的字符串连接成一个大字符串,可以使用 StringBuilder。例如:
String[] fruits = {"苹果", "香蕉", "橙子"};
StringBuilder builder = new StringBuilder();
for (String fruit : fruits) {
builder.append
(fruit).append(", ");
}
String allFruits = builder.toString();
System.out.println(allFruits);格式化输出
要根据特定格式输出数组中的字符串,可以使用 String.format()。例如:
String[] fruits = {"苹果", "香蕉", "橙子"};
System.out.printf("水果列表:%s%n", Arrays.toString(fruits));









