string - How to iterate backwards through an array? -
i want take each value end of array , iterate backwards middle of array, adding each value string:
(int x = testarray.length - 1; x > testarray.length/2; x--){ b+=testarray[x]; }
it doesn't seem work because when print b empty space. possible iterate backwards through array , add values string. string values.
one thing note if array's length odd number, code wont work, since length divided 2 decimal. convert middle index int
. should using stringbuilder
if you're using quite bit of concatenations.
string[] names = {", , henry, ", ", david", "bob"}; stringbuilder allnames = new stringbuilder(); (int = names.length - 1; >= (int) names.length/2; i--) { allnames.append(names[i]); } system.out.println(allnames);
Comments
Post a Comment