Java字符串倒序输出 + 数组的排序

一、Java字符串倒序输出

 

 

方法一:利用String类的toCharArray(),再倒序输出数组。

Java代码

1 public class javatest {

2 public static void main(String[] args) {

3 String originalString = “abcdefg”;

4 String resultString = “”;

5 char[] charArray = originalString.toCharArray();

6 for (int i = charArray.length-1; i》=0; i--){

7 resultString +=charArray[i];

8 }

9 System.out.println(resultString);

10 }

11 }

方法二:如果使用reverse()方法

1 Java代码

2 public class reverseTest {

4 public static void main(String[] args) {

5 String originalString = “abcdefg”;

6 StringBuffer stringBuffer = new StringBuffer(originalString);

7 System.out.println(stringBuffer.reverse());

8 }

9 }

 

===================================================================================================================

 

二、数组的排序

 

import java.util.Arrays;
import java.util.Collections;

/**
* 数组的简单排序正序和倒序
* @author 随想 http://iwgod.com
*/

public class TestArray01 {
public static void main(String[] args) {
test01();
System.out.println();
test02();
}

/**
* 升序
*/

static void test01() {
int [] intArray = new int [] { 4 , 5 , 9 , 0 , 3 , 5 , 6 , 2 };

Arrays.sort(intArray);

for (int i = 0 ; i < intArray.length; i++){
System.out.print(intArray[i]);
}

System.out.println();

String[] stringArray = new String[] { "D" , "E" , "A" , "C" , "B" };

Arrays.sort(stringArray);

for (int i = 0 ; i < stringArray.length; i++){
System.out.print(stringArray[i]);
}
}

/**
* 倒序
*/

static void test02() {
Integer[] intArray = new Integer[] { 4 , 5 , 9 , 0 , 3 , 5 , 6 , 2 };

Arrays.sort(intArray, Collections.reverseOrder());
for (int i = 0 ; i < intArray.length; i++){
System.out.print(intArray[i]);
}

System.out.println();

String[] stringArray = new String[] { "D" , "E" , "A" , "C" , "B" };

Arrays.sort(stringArray, Collections.reverseOrder());

for (int i = 0 ; i < stringArray.length; i++){
System.out.print(stringArray[i]);
}
}
}


你可能感兴趣的:(java)