Issue with printing space with single quotes in a int array in Java -
i want print int[]
in specific format (within space
between each numbers, etc)
however found java not print spaces in single quotes, , change numbers in int[]
. same things happened bracket
character.
for example:
int[] test = {1,2,3,4,5}; system.out.println('(' + test[0]+ ' ' +test[1] + ' ' + test[2] + ' ' +test[3] + ' ' + test[4] + ')'); //224 system.out.println("(" + test[0]+ " " +test[1] + " " + test[2] + " " +test[3] + " " + test[4] + ")"); //(1 2 3 4 5)
i think space
, bracket
characters, , wonder why need use double quotes
print them , why single quotes
space
character change output?
thank you!
the reason is, characters fundamentally stored numbers. when add characters other numbers, you'll receive number result.
// prints 97 system.out.println('a' + 0);
the situation characters not added number when they're added string.
system.out.println("letter" + 'a');
in code above, add numbers , characters together, number output. java adds things left right, changing first-appearing character string, should desired output.
// fixed version system.out.println("(" + test[0]+ ' ' +test[1] + ' ' + test[2] + ' ' +test[3] + ' ' + test[4] + ')');
Comments
Post a Comment