String delimiter = ", ";
The first way is using java.util.StringJoiner
. Additionally, you may specify a prefix and suffix of a result string (for example, (delimiter, "[", "]")
).
StringJoiner joiner = new StringJoiner(delimiter);
for (int i : hourlyTemp) joiner.add(Integer.toString(i));
System.out.println(joiner.toString());
The second way is using Stream API.
Arrays.stream(hourlyTemp)
.map(i -> i.toString())
.collect(Collectors.joining(delimiter));
Or the most boring way:
for (i = 0; i < NUM_VALS;)
System.out.print(hourlyTemp[i] + (i++ != (NUM_VALS - 1) ? delimiter : ""));