1. 程式人生 > >java String字串排序

java String字串排序

1.呼叫reversel()方法實現翻轉

public static String reversel1(String str){

return new StringBuffer(str).reverse().toString();

}


通過new一個新的StringBuffer呼叫自帶方法

2.通過字串陣列實現從尾部開始逐個逆序放入字串中

public static String reverse2(String s)

{

char[] array = s.toCharArray(); //String轉字元陣列

String reverse = ""; //注意這是空串,不是null

for (int i = array.length - 1; i >= 0; i--){

reverse += array[i];

// 即String="reverse",

// 放入時會"r";"e";"v";"e";"r";"s";"e";的放入String.charAt(String.charAt(length--))

}

return reverse;

}

3.通過字串陣列實現從頭部開始逐個正序放入字串中

public static String reverse3(String s)

{

int length = s.length();

String reverse = ""; //注意這是空串,不是null

for (int i = 0; i < length; i++){

reverse = s.charAt(i) + reverse;//在字串前面連線, 而非常見的後面

//即String="reverse",

// 放入時會"e";"s";"r";"e";"v";"e";"r";的放入String。charAt(i++)

//從尾到頭正序放入

}

return reverse;

}

4.C語言中常用的方法:

public static String reverse5(String orig)

{

char[] s = orig.toCharArray();

int n = s.length - 1;

int halfLength = n / 2;

for (int i = 0; i <= halfLength; i++) {

char temp = s[i];

s[i] = s[n - i];

s[n - i] = temp;

}

return new String(s); //知道 char陣列和String相互轉化

}

以上四種轉自

http://blog.sina.com.cn/s/blog_a5ef63c701013dw8.html

5.利用棧的先入後出的特性實現字串的逆轉

public static String reverse5(String string) {

StringBuffer sb = new StringBuffer();

Stack<Character> s = new Stack<Character>();

for (int i = 0; i < string.length(); i++) {

s.add(string.charAt(i));

}

for (int i = 0; i < string.length(); i++) {

sb.append(s.pop());

}

return sb.toString();

}

6.通過二位進位制的右移,也就是/2,實現。與方法4異曲同工

public static String reverse6(String string) {

StringBuffer sb = new StringBuffer(string);

for (int i = 0, j = sb.length() - 1; i < sb.length() >>> 1; i++, j--) {

char temp = sb.charAt(i);

sb.setCharAt(i, sb.charAt(j));

sb.setCharAt(j, temp);

}

return sb.toString();

}

以上兩種轉自

http://blog.csdn.net/hhxin635612026/article/details/26944053

import java.util.Scanner;

import java.util.Stack;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

while(sc.hasNext()){

String str = sc.nextLine();

//逆序翻轉

System.out.println(reverse1(str));

}

}

public static String reverse1(String str){

return new StringBuffer(str).reverse().toString();

}

public static String reverse2(String s)

{

char[] array = s.toCharArray(); //String轉字元陣列

String reverse = ""; //注意這是空串,不是null

for (int i = array.length - 1; i >= 0; i--){

reverse += array[i];

// 即String="reverse",

// 放入時會"r";"e";"v";"e";"r";"s";"e";的放入String.charAt(String.charAt(length--))

}

return reverse;

}

public static String reverse3(String s)

{

int length = s.length();

String reverse = ""; //注意這是空串,不是null

for (int i = 0; i < length; i++){

reverse = s.charAt(i) + reverse;//在字串前面連線, 而非常見的後面

//即String="reverse",

// 放入時會"e";"s";"r";"e";"v";"e";"r";的放入String。charAt(i++)

//從尾到頭正序放入

}

return reverse;

}

public static String reverse4(String orig)

{

char[] s = orig.toCharArray();

int n = s.length - 1;

int halfLength = n / 2;

for (int i = 0; i <= halfLength; i++) {

char temp = s[i];

s[i] = s[n - i];

s[n - i] = temp;

}

return new String(s); //知道 char陣列和String相互轉化

}

public static String reverse5(String string) {

StringBuffer sb = new StringBuffer();

Stack<Character> s = new Stack<Character>(); //new一個棧

for (int i = 0; i < string.length(); i++) {

s.add(string.charAt(i));

//往棧中逐個正序放入字元型string.charAt(i++);

}

for (int i = 0; i < string.length(); i++) {

sb.append(s.pop());

//棧取出的順序與放入的順序相反,所以實現逆序

//length=string.length();

// 逐個增加string.charAt(length--);

}

return sb.toString();

}

public static String reverse6(String string) {

StringBuffer sb = new StringBuffer(string);

for (int i = 0, j = sb.length() - 1; i < sb.length() >>> 1; i++, j--) {

char temp = sb.charAt(i);

sb.setCharAt(i, sb.charAt(j));

sb.setCharAt(j, temp);

}

return sb.toString();

}

public static String reverse7(String string) {

StringBuffer sb = new StringBuffer();

for (int i = string.length() - 1; i >= 0; i--) {

sb.append(string.charAt(i));

}

return sb.toString();

}

}