- public class T {
public static void main(String[] args) {
StringBuffer sbf = new StringBuffer();
sbf.append("abcdefghick123456");
System.out.println(sbf.reverse().toString());
}
}//字符串的倒序输出
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; - public class T {
public void order() throws IOException {
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
String str = br.readLine();
char c[] = str.toCharArray();
int i = str.length();
i--;
char temp;
for (int j = 0; j < i; j++, i--) {
temp = c[j];
c[j] = c[i];
c[i] = temp;
}
System.out.println(new String(c, 0, str.length()));
} - public static void main(String[] args) throws IOException {
new T().order();
}
}//输入的倒序输出 -
public class T {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = array.length - 1; i >= 0; i--) {
System.out.print(i);
}
}
}//字符串的倒序输出
=====================================
- import java.lang.StringBuffer;
- public class API_String {
- public static void main(String[] args) {
- String str = "abcd";
- // 字符串倒序方法一
char[] ch = str.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = ch.length - 1; i >= 0; i--) {
sb.append(ch[i]);
}
System.out.println(sb.toString()); - // 字符串倒序方法二
StringBuffer sbb = new StringBuffer();
for (int i = str.length() - 1; i >= 0; i--) {
sbb.append(str.charAt(i));
}
System.out.println(sbb.toString()); - // 字符串倒序方法三
System.out.println(new StringBuffer(str).reverse().toString());
} - }
- 方法一和方法二都是利用已知的索引进行倒序,它们的算法很类似。方法三利用了JAVA中已有API的倒序方法(StringBuffer中的reverse())在API帮助文档里是这么说的:将此字符序列用其反转形式取代。