字符串方法与整数数组

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

String method with array of integers

问题

请帮忙修改代码。我尝试过添加子字符串等方法来去除末尾的逗号。我该如何操作?
例如:将{1,2,3}转换为字符串后变成了"1,2,3,"

  1. public static String arrayToString(int[] numbers)
  2. {
  3. String stringify = "";
  4. for (int index = 0; index < numbers.length; index++)
  5. {
  6. stringify += numbers[index] + ", ";
  7. }
  8. return stringify;
  9. }
英文:

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;

  1. public static String arrayToString(int[] numbers)
  2. {
  3. String stringify = &quot;&quot;;
  4. for (int index = 0; index &lt; numbers.length; index++)
  5. {
  6. stringify = numbers[index] + &quot;, &quot;;
  7. }
  8. return stringify;
  9. }

答案1

得分: 2

最佳方式

删除整个方法。改用 Arrays.toString(int[])。为什么要重新编写核心功能呢?

Stream方式

  1. return Arrays.stream(numbers)
  2. .boxed()
  3. .map(String::valueOf)
  4. .collect(Collectors.joining(", "));

StringBuilder方式

  1. StringBuilder out = new StringBuilder();
  2. for (int i = 0; i < numbers.length; i++) {
  3. if (i != 0) out.append(", ");
  4. out.append(numbers[i]);
  5. }
  6. return out.toString();

在循环中追加字符串是不好的做法(会浪费大量内存),所以不要这样做。上述策略的偏好顺序大致如上,最佳选项遥遥领先于其他方法。

英文:

Best way

Delete the entire method. Use Arrays.toString(int[]) instead. Why rewrite core functionality like this?

Stream way

  1. return Arrays.stream(numbers)
  2. .boxed()
  3. .map(String::valueOf)
  4. .collect(Collectors.joining(&quot;, &quot;));

StringBuilder way

  1. StringBuilder out = new StringBuilder();
  2. for (int i = 0; i &lt; numbers.length; i++) {
  3. if (i != 0) out.append(&quot;, &quot;);
  4. out.append(numbers[i]);
  5. }
  6. 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

你可以将方法修改为以下形式:

  1. public static String arrayToString(int[] numbers)
  2. {
  3. String stringify = "";
  4. for (int index = 0; index < numbers.length; index++)
  5. {
  6. stringify = numbers[index];
  7. if (index < numbers.length - 1) {
  8. stringify += ", ";
  9. }
  10. }
  11. return stringify;
  12. }
英文:

You can modify your method to the following one:

  1. public static String arrayToString(int[] numbers)
  2. {
  3. String stringify = &quot;&quot;;
  4. for (int index = 0; index &lt; numbers.length; index++)
  5. {
  6. stringify = numbers[index];
  7. if (index &lt; numbers.length - 1) {
  8. stringify += &quot;, &quot;;
  9. }
  10. }
  11. return stringify;
  12. }

答案3

得分: 0

  1. 我倾向于将其视为在每个数字之前都有一个分隔符除了第一个数字之外或者同样地每个数字之前都有一个分隔符但第一个分隔符是空的
  2. public static String arrayToString(int[] numbers)
  3. {
  4. String separator = "";
  5. String stringify = "";
  6. for (int index = 0; index < numbers.length; index++)
  7. {
  8. stringify += separator + numbers[index];
  9. separator = ", ";
  10. }
  11. return stringify;
  12. }
  13. 注意我将 `stringify` 的赋值修正为 `+=` 而不是 `=`因为否则你只会得到最后一个数字
  14. 实际上在实践中我可能会使用 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.

  1. public static String arrayToString(int[] numbers)
  2. {
  3. String separator = &quot;&quot;;
  4. String stringify = &quot;&quot;;
  5. for (int index = 0; index &lt; numbers.length; index++)
  6. {
  7. stringify += separator + numbers[index];
  8. separator = &quot;, &quot;
  9. }
  10. return stringify;
  11. }

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:

确定