gpt4 book ai didi

Java 打印一个带有 ", "符号的整个数组?

转载 作者:搜寻专家 更新时间:2023-11-01 01:01:31 27 4
gpt4 key购买 nike

System.out.print("Please select a game: ");
for (String s : gamesArray) {
System.out.print(s + ", ");
}

输出:

Please select a game: spin, tof, Press any key to exit...

我排除的输出:

Please select a game: spin, tofPress any key to exit...

为什么在最后一个数组项之后添加另一个','? 我该如何预防?

最佳答案

为什么不直接调用Arrays#toString(array):

System.out.print("Please select a game: %s%n", 
Arrays.toString(gamesArray).replaceAll("(^\\[)|(\\]$)", ""));

或避免使用正则表达式:

String tmp = Arrays.toString(gamesArray);
System.out.print("Please select a game: %s%n", tmp.substring(1, tmp.length()-1));

关于Java 打印一个带有 ", "符号的整个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17284121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com