一、数组实现String反转
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//数组实现String反转
public String reverseByArray(){
if (str == null || str.length() == 1){
return null ;
}
char[] ch = str.toCharArray(); //字符串转换成字符数组
for (int i = 0 ; i < ch.length/2 ; i++){
char temp = ch[i];
ch[i] = ch[ch.length-i-1];
ch[ch.length-i-1] = temp;
}
return new String(ch);
}
|
二、栈实现String反转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//用栈实现String反转
public String reverseByStack(){
if (str == null || str.length() == 1){
return null ;
}
Stack<Character> stack = new Stack<Character>();
char[] ch = str.toCharArray(); //字符串转换成字符数组
for (char c : ch) {
stack.push(c); //每个字符,推进栈
}
for (int i = 0; i < ch.length; i++) {
ch[i] = stack.pop(); //移除这个堆栈的顶部对象
}
return new String(ch);
}
|
三、逆序遍历实现String反转
1
2
3
4
5
6
7
8
9
10
11
12
|
//用逆序遍历实现String反转
public String reverseBySort(){
if (str == null || str.length() == 1){
return null ;
}
StringBuffer sb = new StringBuffer();
for (int i = str.length() -1 ; i >= 0; i--) {
sb.append(str.charAt(i)); //使用StringBuffer从右往左拼接字符
}
return sb.toString();
}
|
四、位运算实现String反转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//使用位运算实现String反转
public String reverseByBit() {
if (str == null || str.length() == 1){
return null ;
}
char[] ch = str.toCharArray(); //字符串转换成字符数组
int len = str.length();
for (int i= 0; i< len/ 2; i++) {
ch[i]^= ch[len- 1- i];
ch[len- 1- i]^= ch[i];
ch[i]^= ch[len- 1- i];
}
return new String(ch);
}
|
五、递归实现String反转
1
2
3
4
5
6
7
8
9
10
11
12
|
//使用递归实现String反转
public String reverseByRecursive(String str){
if (str == null || str.length() == 0 ){
return null ;
}
if (str.length() == 1 ){
return str;
} else {
//从下标为1开始截取字符串,在返回下标为0的字符
return reverseByRecursive(str.substring( 1 )) + str.charAt( 0 );
}
}
|
六、测试
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Test {
public static void main(String[] args) {
String s = "123456" ;
Reverse r = new Reverse(s);
System.out.println(r.reverseByArray());
System.out.println(r.reverseByStack());
System.out.println(r.reverseBySort());
System.out.println(r.reverseByBit());
System.out.println(r.reverseByRecursive(s));
}
}
|
七、结果
八、用于String反转的全部代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
public class Reverse {
private String str = null ;
public Reverse(String str){
this .str = str;
}
//数组实现String反转
public String reverseByArray(){
if (str == null || str.length() == 1 ){
return null ;
}
char [] ch = str.toCharArray(); //字符串转换成字符数组
for ( int i = 0 ; i < ch.length/ 2 ; i++){
char temp = ch[i];
ch[i] = ch[ch.length-i- 1 ];
ch[ch.length-i- 1 ] = temp;
}
return new String(ch);
}
//用栈实现String反转
public String reverseByStack(){
if (str == null || str.length() == 1 ){
return null ;
}
Stack<Character> stack = new Stack<Character>();
char [] ch = str.toCharArray(); //字符串转换成字符数组
for ( char c : ch) {
stack.push(c); //每个字符,推进栈
}
for ( int i = 0 ; i < ch.length; i++) {
ch[i] = stack.pop(); //移除这个堆栈的顶部对象
}
return new String(ch);
}
//用逆序遍历实现String反转
public String reverseBySort(){
if (str == null || str.length() == 1 ){
return null ;
}
StringBuffer sb = new StringBuffer();
for ( int i = str.length() - 1 ; i >= 0 ; i--) {
sb.append(str.charAt(i)); //使用StringBuffer从右往左拼接字符
}
return sb.toString();
}
//使用位运算实现String反转
public String reverseByBit() {
if (str == null || str.length() == 1 ){
return null ;
}
char [] ch = str.toCharArray(); //字符串转换成字符数组
int len = str.length();
for ( int i= 0 ; i< len/ 2 ; i++) {
ch[i]^= ch[len- 1 - i];
ch[len- 1 - i]^= ch[i];
ch[i]^= ch[len- 1 - i];
}
return new String(ch);
}
//使用递归实现String反转
public String reverseByRecursive(String str){
if (str == null || str.length() == 0 ){
return null ;
}
if (str.length() == 1 ){
return str;
} else {
//从下标为1开始截取字符串,在返回下标为0的字符
return reverseByRecursive(str.substring( 1 )) + str.charAt( 0 );
}
}
}
|
以上这篇Java中5种方式实现String反转就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。