英文:
String method with array of integers
问题
请帮忙修改代码。我尝试过添加子字符串等方法来去除末尾的逗号。我该如何操作?
例如:将{1,2,3}
转换为字符串后变成了"1,2,3,"
。
public static String arrayToString(int[] numbers)
{
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify += numbers[index] + ", ";
}
return stringify;
}
英文:
Please help with code. I have tried adding substring, etc, to remove the end comma. How can I do this?
Example: {1,2,3}
to string is now "1,2,3,"
public static String arrayToString(int[] numbers)
{
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify = numbers[index] + ", ";
}
return stringify;
}
答案1
得分: 2
最佳方式
删除整个方法。改用 Arrays.toString(int[])
。为什么要重新编写核心功能呢?
Stream方式
return Arrays.stream(numbers)
.boxed()
.map(String::valueOf)
.collect(Collectors.joining(", "));
StringBuilder方式
StringBuilder out = new StringBuilder();
for (int i = 0; i < numbers.length; i++) {
if (i != 0) out.append(", ");
out.append(numbers[i]);
}
return out.toString();
在循环中追加字符串是不好的做法(会浪费大量内存),所以不要这样做。上述策略的偏好顺序大致如上,最佳选项遥遥领先于其他方法。
英文:
Best way
Delete the entire method. Use Arrays.toString(int[])
instead. Why rewrite core functionality like this?
Stream way
return Arrays.stream(numbers)
.boxed()
.map(String::valueOf)
.collect(Collectors.joining(", "));
StringBuilder way
StringBuilder out = new StringBuilder();
for (int i = 0; i < numbers.length; i++) {
if (i != 0) out.append(", ");
out.append(numbers[i]);
}
return out.toString();
Adding to a string in a loop is bad form (it wastes a ton of memory), so don't do that. The above strategies are in rough order of preference, with the top option miles ahead of the rest.
答案2
得分: 0
你可以将方法修改为以下形式:
public static String arrayToString(int[] numbers)
{
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify = numbers[index];
if (index < numbers.length - 1) {
stringify += ", ";
}
}
return stringify;
}
英文:
You can modify your method to the following one:
public static String arrayToString(int[] numbers)
{
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify = numbers[index];
if (index < numbers.length - 1) {
stringify += ", ";
}
}
return stringify;
}
答案3
得分: 0
我倾向于将其视为在每个数字之前都有一个分隔符,除了第一个数字之外。或者,同样地,每个数字之前都有一个分隔符,但第一个分隔符是空的。
public static String arrayToString(int[] numbers)
{
String separator = "";
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify += separator + numbers[index];
separator = ", ";
}
return stringify;
}
注意,我将 `stringify` 的赋值修正为 `+=` 而不是 `=`,因为否则你只会得到最后一个数字。
实际上,在实践中,我可能会使用 StringBuilder,但我想尽量保持原样。使用 StringBuilder,可以使用相同的分隔符技术。
英文:
I prefer to view this as having a separator before every number except for the first. Or, equally, that there's a separator before every number, but the first separator is empty.
public static String arrayToString(int[] numbers)
{
String separator = "";
String stringify = "";
for (int index = 0; index < numbers.length; index++)
{
stringify += separator + numbers[index];
separator = ", "
}
return stringify;
}
Note, I fixed the assignment to stringify
to be +=
rather than =
, since otherwise you're just getting the last number.
In practice, I'd probably use a StringBuilder, but I wanted to leave this as close to the original as possible. With a StringBuilder, the same separator technique can be used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论