+ 2

Java code

Anyone know why my this code print [, , , , , , , , , , , , , , , , , ,] https://code.sololearn.com/ctgE332b8V81/?ref=app

4th Jun 2019, 8:57 AM
Tzion
Tzion - avatar
4 Respuestas
+ 4
Because ans2 returns a string, but you add nothing to s, therefore it stays as an empty string, and that will be added to the arraylist. I think you wanted to do s += x + " "; instead of System.out.println(x, " ");
4th Jun 2019, 9:04 AM
Airree
Airree - avatar
+ 3
The string returned by the call to ans2 is empty, and it is added to the arraylist. The string representation of an arraylist contains elements printed one after the other inside square brackets seperated by commas. Since the elements are empty, you get and arraylist of empty strings separated by commas.
4th Jun 2019, 9:14 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Wang Teng Zheng I don't know the expected output, so maybe you can try what Airree suggested and see if that works out for you. If you wish to achieve something else, kindly provide an explanation regarding the same.
4th Jun 2019, 10:59 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
0
Kinshuk Vasisht did u know anywhere instead of arraylist to return the ans
4th Jun 2019, 9:16 AM
Tzion
Tzion - avatar