字符串方法与整数数组

huangapple go评论82阅读模式
英文:

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 &quot;1,2,3,&quot;

public static String arrayToString(int[] numbers) 
{
  String stringify = &quot;&quot;;
  for (int index = 0; index &lt; numbers.length; index++) 
  {
    stringify = numbers[index] + &quot;, &quot;;
  }
  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(&quot;, &quot;));

StringBuilder way

StringBuilder out = new StringBuilder();
for (int i = 0; i &lt; numbers.length; i++) {
    if (i != 0) out.append(&quot;, &quot;);
    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 = &quot;&quot;;
  for (int index = 0; index &lt; numbers.length; index++) 
  {
    stringify = numbers[index];
    if (index &lt; numbers.length - 1) {
        stringify += &quot;, &quot;;
    }
  }
  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 = &quot;&quot;;
  String stringify = &quot;&quot;;
  for (int index = 0; index &lt; numbers.length; index++) 
  {
    stringify += separator + numbers[index];
    separator = &quot;, &quot;
  }
  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.

huangapple
  • 本文由 发表于 2020年10月14日 05:21:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64343319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定