public class Test { public static void main (String[] args) throws Exception { String s = "abcdefg"; System.out.println (s); s = new StringBuilder(s).reverse().toString(); System.out.println (s); } }
public class Test { public static void main (String[] args) throws Exception { String[] str={"6","5","3","1","2","4"}; Arrays.sort(str); for (String s : str) System.out.print (s + " "); } }
}2.public static void main(String[] args) throws Exception { String[] str = { "6", "5", "3", "1", "2", "4" }; for (int i = 0; i < str.length - 1;) { String a = ""; int a1 = Integer.parseInt(str[i]); int a2 = Integer.parseInt(str[i + 1]); if (a1 > a2) { a = str[i]; str[i] = str[i + 1]; str[i + 1] = a; if (i - 1 >= 0) { i--; } } else { i++; } } for (int i = 0; i < str.length; i++) { System.out.print(str[i]); } }
public class Test { public static void main(String[] args) { String str = "abcdefg"; System.out.println(reverseMothed1(str)); System.out.println(reverseMothed2(str)); }
public static String reverseMothed1(String str) { if(str == null) { return str; } char[] c = str.toCharArray(); char[] temp = new char[c.length]; for(int i = c.length - 1, j = 0; i >= 0; i--, j++) { temp[j] = c[i]; } return new String(temp); }
public class lianxi1 { public static void main(String[] args) { String s = "abcdefg"; byte[] bytes = s.getBytes();
//第一种: //加个-号进行排序 for(int i = 0;i < bytes.length;i++){ bytes[i] = (byte) -bytes[i]; } Arrays.sort(bytes); //排序完后,加负号变回来 for(int i = 0;i < bytes.length;i++){ bytes[i] = (byte) -bytes[i]; } s = new String(bytes);
/*//第二种: byte[] bytes1 = new byte[bytes.length]; for(int i = 0;i < bytes.length;i++){ bytes1[i] = bytes[bytes.length-i-1]; } s = new String(bytes1);*/ System.out.println(s); } }
可以的,参考1楼的办法,自己写个Comparator,就行了.
不好意思好像不行!确实要实现Comparator接口!2楼的写得还可以吧
// nothing is impossible public class Test { public static void main (String[] args) throws Exception { String[] str={"6","5","3","1","2","4"}; Arrays.sort(str, new Comparator<String>(){ public int compare(String s1, String s2) { return Integer.parseInt(s1) - Integer.parseInt(s2); } }); for (String s : str) System.out.print (s + " "); } }
import java.util.Comparator;public class Test { public static void main(String[] args) {
Comparator<String> com = new Comparator<String>(){
public int compare(String o1, String o2) {
return Integer.valueOf(o1)-Integer.valueOf(o2);
}
};
String[] strs = {"6","5","3","1","2","4"};
Arrays.sort(strs, com);
for(String s:strs){
System.out.println(s);
}
}
}
1.用StringBuilder的reverse方法(StringBuffer也有,算不算第二种?)
2.用toCharArray()转成数组操作.
2.1,头,尾两个指针,交换两个字符,头++.尾--,循环条件:头<尾。
2.2。用栈的思想,顺序进一次栈,再依次出栈。
3.转成数组,再用Arrays.asList()转成List,List也有一个reverse方法。List用toString转为字符串,再把其中的空格和,[]去掉。
public static void main (String[] args) throws Exception {
String s = "abcdefg";
System.out.println (s);
s = new StringBuilder(s).reverse().toString();
System.out.println (s);
}
}
public static void main (String[] args) throws Exception {
String[] str={"6","5","3","1","2","4"};
Arrays.sort(str);
for (String s : str)
System.out.print (s + " ");
}
}
String a = "abcdefg";
char b[] = a.toCharArray();
for(int i=b.length-1;i>=0;i--)
{
System.out.print(b[i]);
}
}2.public static void main(String[] args) throws Exception {
String[] str = { "6", "5", "3", "1", "2", "4" };
for (int i = 0; i < str.length - 1;) {
String a = "";
int a1 = Integer.parseInt(str[i]);
int a2 = Integer.parseInt(str[i + 1]);
if (a1 > a2) {
a = str[i];
str[i] = str[i + 1];
str[i + 1] = a;
if (i - 1 >= 0) {
i--;
}
} else {
i++;
}
}
for (int i = 0; i < str.length; i++) {
System.out.print(str[i]);
}
}
public class Test {
public static void main(String[] args) {
String str = "abcdefg";
System.out.println(reverseMothed1(str));
System.out.println(reverseMothed2(str));
}
public static String reverseMothed1(String str) {
if(str == null) {
return str;
}
char[] c = str.toCharArray();
char[] temp = new char[c.length];
for(int i = c.length - 1, j = 0; i >= 0; i--, j++) {
temp[j] = c[i];
}
return new String(temp);
}
public static String reverseMothed2(String str) {
if(str == null) {
return str;
}
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
}
"111"<"12"的
public class lianxi1 {
public static void main(String[] args) {
String s = "abcdefg";
byte[] bytes = s.getBytes();
//第一种:
//加个-号进行排序
for(int i = 0;i < bytes.length;i++){
bytes[i] = (byte) -bytes[i];
}
Arrays.sort(bytes);
//排序完后,加负号变回来
for(int i = 0;i < bytes.length;i++){
bytes[i] = (byte) -bytes[i];
}
s = new String(bytes);
/*//第二种:
byte[] bytes1 = new byte[bytes.length];
for(int i = 0;i < bytes.length;i++){
bytes1[i] = bytes[bytes.length-i-1];
}
s = new String(bytes1);*/
System.out.println(s);
}
}
// nothing is impossible
public class Test {
public static void main (String[] args) throws Exception {
String[] str={"6","5","3","1","2","4"};
Arrays.sort(str, new Comparator<String>(){
public int compare(String s1, String s2) {
return Integer.parseInt(s1) - Integer.parseInt(s2);
}
});
for (String s : str)
System.out.print (s + " ");
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class Sort {
public static void main(String[] args) {
//字符串“abcdefg”倒转排序。
String string="abcdefg";
ArrayList<String>list=new ArrayList<String>();
for(int i=0;i<string.length();i++)
list.add(""+string.charAt(i));
Comparator cmp=Collections.reverseOrder();
Collections.sort(list, cmp);
for(String s:list)
System.out.println(s);