如何格式化这个输出?

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

How to format this output?

问题

I have created a method, which is supposed to return a growing number list when entered any number.
For example:

input: 5

expected output: [1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5]

current output: [1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5]

input: 2

expected output: [1, 2 2]

current output: [1, 2 2]

How do I delete the white spaces?
This is my code:

ArrayList<String> maxarray = new ArrayList<String>();
int i, j;
String num = "";

for (i = 1; i <= max; i++) {
    num = "";
    for (j = 1; j <= i; j++) {
        num = num + i + " ";
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;

I have tried: num = num.replace(" ", ""); num = num.replace(" ", ""); but, they don't seem to work. and if I try to convert it into a string, I get the following output: 1 2 2 3 3 3

Help me, please

英文:

I have created a method, which is supposed to return a growing number list when entered any number.
For example:

input: 5

expected output: [1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5]

current output : [1 , 2 2 , 3 3 3 , 4 4 4 4 , 5 5 5 5 5 ]

input: 2

expected output: [1, 2 2]

current output : [1 , 2 2 ]

How do I delete the white spaces?
This is my code:

ArrayList &lt; String &gt; maxarray = new ArrayList &lt; String &gt; ();
int i, j;
String num = &quot;&quot;;

for (i = 1; i &lt;= max; i++) {
    num = &quot;&quot;;
    for (j = 1; j &lt;= i; j++) {
        num = num + i + &quot; &quot;;
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;

I have tried: num= num.replace(&quot; &quot;,&quot;&quot;);
num = num.replace(&quot; &quot;, &quot;&quot;);
but, they don't seem work.
and if I try to convert it into a string, I get the following output: 1 2 2 3 3 3

Help me, please

答案1

得分: 3

简单而短视的解决方案: 在将字符串添加到 ArrayList 之前,只需修剪字符串:

maxarray.add(num.trim());

通过这个更改,输出变为:

[1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5]

trim() 会移除尾随空格(如果有的话,也会移除前导空格),但不会移除数字之间的空格。

从一开始就产生正确的输出: 但是,与其产生不完全正确的输出然后再进行更正,从一开始就生成正确的输出可能会更不容易混淆。也就是说,在最后一个数字后面不要有空格。这也是 Berto99 的回答中所做的。我更喜欢的方法是:在内部循环中减少一次迭代,并且避免在那里添加空格。

for (i = 1; i <= max; i++) {
    num = "";
    for (j = 1; j < i; j++) { // 在 i 之前停止一个数字
        num = num + i + " ";
    }
    num = num + i; // 在这里不要添加空格
    maxarray.add(num);
}

输出仍然是:

[1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5]

相比 Berto99 回答中的额外的 if 语句版本,我认为这种方法更易阅读。

为了精确起见: maxarray 不是一个整数数组,它是一个包含字符串的 ArrayList。我们需要修剪的不是 ArrayList 本身,而是其中的每个字符串。

PS 流版本: 对于了解并喜欢流的读者,这是一个流版本。如果您还不想学习流,可以忽略这部分。

List<String> maxarray = IntStream.rangeClosed(1, max)
        .mapToObj(i -> IntStream.rangeClosed(1, i)
                .mapToObj(j -> String.valueOf(i))
                .collect(Collectors.joining(" ")))
        .collect(Collectors.toList());

其中方便的一点是,Collectors.joining(" ") 会在数字之间添加空格,而不会在第一个数字之前或最后一个数字之后添加空格,从而解决了提出的问题。

英文:

Easy and short-sighted solution: just trim the string before adding it to the ArrayList:

	    maxarray.add(num.trim());

With this change the output is:

> [1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5]

trim() removes trailing spaces (and leading, had there been any), but not the spaces between the numbers.

Produce correct output from the outset: However, rather than producing not-quite-right output and then correcting it it’s probably less confusing in the end to produce correct output from the outset. That is, without the space after the last number. This is also what the answer by Berto99 does. My preferred way of doing this is: take one iteration out of the inner loop and refrain from adding the space there.

	for (i = 1; i &lt;= max; i++) {
	    num = &quot;&quot;;
	    for (j = 1; j &lt; i; j++) { // stop one number before i
	        num = num + i + &quot; &quot;;
	    }
	    num = num + i; // add no space here
	    maxarray.add(num);
	}

Output still is:

> [1, 2 2, 3 3 3, 4 4 4 4, 5 5 5 5 5]

I find this way easier to read than the version with the extra if statement in the answer by Berto99.

For the sake of precision: maxarray is not an int array, it as an ArrayList of strings. And what we needed to trim was not the ArrayList but each string in it.

PS Stream version: For readers who know and like streams here’s a stream version. If you don’t want to learn about streams yet, just ignore.

	List&lt;String&gt; maxarray = IntStream.rangeClosed(1, max)
			.mapToObj(i -&gt; IntStream.rangeClosed(1, i)
					.mapToObj(j -&gt; String.valueOf(i))
					.collect(Collectors.joining(&quot; &quot;)))
			.collect(Collectors.toList());

One of the convenient things is that Collectors.joining(&quot; &quot;) puts spaces between the numbers without putting any space before the first or after the last, which solves the problem asked about.

答案2

得分: 1

正如您从这里所看到的:

ArrayList<String> maxarray = new ArrayList<String>();
int i, j;
String num = "";

for (i = 1; i <= max; i++) {
    num = "";
    for (j = 1; j <= i; j++) {
        num = num + i + " ";
                        ^^^^-- 这里
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;

您在每个数字后面添加了一个空格,所以您只需要检查是否不在最后一个数字,以免在那种情况下添加空格:

ArrayList<String> maxarray = new ArrayList<String>();
int i, j;
String num = "";

for (i = 1; i <= max; i++) {
    num = "";
    for (j = 1; j <= i; j++) {
        num = num + i;
        if (j < i) // <--- 检查是否不是最后一次迭代
           num = num + " ";
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;
英文:

As you can see from here:

ArrayList &lt; String &gt; maxarray = new ArrayList &lt; String &gt; ();
int i, j;
String num = &quot;&quot;;

for (i = 1; i &lt;= max; i++) {
    num = &quot;&quot;;
    for (j = 1; j &lt;= i; j++) {
        num = num + i + &quot; &quot;;
                        ^^^^-- here
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;

you add a space after every number, so you only need to check if you are not in the last number, in order not to add the space in that case:

ArrayList &lt; String &gt; maxarray = new ArrayList &lt; String &gt; ();
int i, j;
String num = &quot;&quot;;

for (i = 1; i &lt;= max; i++) {
    num = &quot;&quot;;
    for (j = 1; j &lt;= i; j++) {
        num = num + i;
        if(j &lt; i) // &lt;--- check if you are not in the last iteration
           num = num + &quot; &quot;;
    }
    //System.out.print(i);
    maxarray.add(num);
}
return maxarray;

</details>



huangapple
  • 本文由 发表于 2020年7月25日 09:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63083340.html
匿名

发表评论

匿名网友

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

确定