How can I print a comma separated list on 1 line with the last number ending without a comma/space/new line using a FOR loop?

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

How can I print a comma separated list on 1 line with the last number ending without a comma/space/new line using a FOR loop?

问题

for (i = 0; i < hourlyTemp.length; i++) {
    System.out.print(hourlyTemp[i]);
    if (i < hourlyTemp.length - 1) {
        System.out.print(", ");
    }
}

这会给你想要的输出,但是末尾会有一个逗号。如何在末尾没有逗号?使用一个 for 循环是否可行?

英文:
for (i = 0; i &lt; hourlyTemp.length; i++) {
    System.out.print(hourlyTemp[i] + &quot;, &quot;);
     
}

this would give me the output that I want but with a comma the end. How can I do this without the comma at the end? is it even possible with a for-loop?

答案1

得分: 1

容易极了。

for(int i = 0; i < hourlyTemp.length; i++) {
    System.out.print(hourlyTemp[i]);
    if(i == hourlyTemp.length - 1) break;
    System.out.print(",");
}
英文:

Easy peasy.

for(int i = 0; i &lt; hourlyTemp.length; i++) {
    System.out.print(hourlyTemp[i]);
    if(i == hourlyTemp.length -1) break;
    System.out.print(&quot;,&quot;);
}

答案2

得分: 0

你可以添加一个 if 语句来打印逗号:

for (i = 0; i < hourlyTemp.length; i++) {
    System.out.print(hourlyTemp[i]); 
    if(i != (hourlyTemp.length - 1)){
        System.out.print(", ");
    }
}
英文:

you could add an if statement for printing comma:

for (i = 0; i &lt; hourlyTemp.length; i++) {
    System.out.print(hourlyTemp[i]); 
    if(i != (hourlyTemp.length - 1)){
        System.out.print(&quot;, &quot;);
    }
}

答案3

得分: 0

如果您不希望在循环内部使用 if 条件来避免在较大数组的情况下进行条件检查您可以使用以下代码

    for (i = 0; i < hourlyTemp.length - 1; i++)
      {
	    System.out.print (hourlyTemp[i] + ", ");
      }

    System.out.print (hourlyTemp[hourlyTemp.length - 1]);
英文:

If you don't want if condition inside a loop to avoid condition checking in case of larger array, you can use code below:

for (i = 0; i &lt; hourlyTemp.length - 1; i++)
  {
    System.out.print (hourlyTemp[i] + &quot;, &quot;);
  }

System.out.print (hourlyTemp[hourlyTemp.length - 1]);

答案4

得分: 0

我发现抑制前导逗号比抑制尾随逗号更容易,因为我们不必使用 length 来实现:

for (i = 0; i < hourlyTemp.length; i++) {
    if (i != 0)
        System.out.print(", ");
    System.out.print(hourlyTemp[i]);
}

使用增强型 for 循环,你需要一个标志:

boolean first = true;
for (double temp : hourlyTemp) {
    if (first)
        first = false;
    else
        System.out.print(", ");
    System.out.print(temp);
}

你还可以构建一个包含整行内容的 String。这样做的性能更好,因为 print() 会带来一些开销:

StringBuilder buf = new StringBuilder();
for (double temp : hourlyTemp)
    buf.append(", ").append(temp);
if (buf.length() != 0)
    System.out.print(buf.substring(2));

使用 Java 8 中添加的 StringJoiner 更加简单:

StringJoiner buf = new StringJoiner(", ");
for (double temp : hourlyTemp)
    buf.add(String.valueOf(temp));
System.out.print(buf.toString());
英文:

I find that suppressing a leading comma, instead of a trailing comma, is easier, since we don't have to use length to do so:

for (i = 0; i &lt; hourlyTemp.length; i++) {
    if (i != 0)
        System.out.print(&quot;, &quot;);
    System.out.print(hourlyTemp[i]);
}

With a for-each loop, you'd need a flag:

boolean first = true;
for (double temp : hourlyTemp) {
    if (first)
        first = false;
    else
        System.out.print(&quot;, &quot;);
    System.out.print(temp);
}

You can also build a String with the entire line. That would perform better, since print() has some overhead:

StringBuilder buf = new StringBuilder();
for (double temp : hourlyTemp)
    buf.append(&quot;, &quot;).append(temp);
if (buf.length() != 0)
    System.out.print(buf.substring(2));

Using the StringJoiner added in Java 8 is even easier:

StringJoiner buf = new StringJoiner(&quot;, &quot;);
for (double temp : hourlyTemp)
    buf.add(String.valueOf(temp));
System.out.print(buf.toString());

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

发表评论

匿名网友

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

确定