Well, it looks like you just want:
for(int i = 10; i >= 0; i--){ System.out.println(i); System.out.println(10 - i); }
This is true? Personally, I would usually write this as a growing cycle, as it was easier for me to think about this:
for (int i = 0; i <= 10; i++) { System.out.println(10 - i); System.out.println(i); }
Note that your string example is really inefficient, by the way - much more than introducing an extra variable. Given that you know how long to start, you can start with two char[] right size and fill in the right index each time. Then create a line from each afterwards. Again, I would do this with an increase in the loop:
char[] forwardChars = new char[str.length()]; char[] reverseChars = new char[str.length()]; for (int i = 0; i < str.length(); i++) { forwardChars[i] = str.charAt(i); reverseChars[reverseChars.length - i - 1] = str.charAt(i); } String forwardString = new String(forwardChars); String reverseString = new String(reverseChars);
(Of course, forwardString in this case will be str anyway ...)
source share