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 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