java编程:任意给出一个字符串数组,按照字母的顺序将其排序输出。

可用排序算法 compareTo()
2024-12-02 04:40:58
推荐回答(5个)
回答1:

//JAVA原装的String比较方法
/**
* Compares two strings lexicographically.
* The comparison is based on the Unicode value of each character in
* the strings. The character sequence represented by this
* String object is compared lexicographically to the
* character sequence represented by the argument string. The result is
* a negative integer if this String object
* lexicographically precedes the argument string. The result is a
* positive integer if this String object lexicographically
* follows the argument string. The result is zero if the strings
* are equal; compareTo returns 0 exactly when
* the {@link #equals(Object)} method would return true.
*


* This is the definition of lexicographic ordering. If two strings are
* different, then either they have different characters at some index
* that is a valid index for both strings, or their lengths are different,
* or both. If they have different characters at one or more index
* positions, let k be the smallest such index; then the string
* whose character at position k has the smaller value, as
* determined by using the < operator, lexicographically precedes the
* other string. In this case, compareTo returns the
* difference of the two character values at position k in
* the two string -- that is, the value:
*


* this.charAt(k)-anotherString.charAt(k)
*

* If there is no index position at which they differ, then the shorter
* string lexicographically precedes the longer string. In this case,
* compareTo returns the difference of the lengths of the
* strings -- that is, the value:
*

* this.length()-anotherString.length()
*

*
* @param anotherString the String to be compared.
* @return the value 0 if the argument string is equal to
* this string; a value less than 0 if this string
* is lexicographically less than the string argument; and a
* value greater than 0 if this string is
* lexicographically greater than the string argument.
*/
public int compareTo(String anotherString) {
int len1 = count;
int len2 = anotherString.count;
int n = Math.min(len1, len2);
char v1[] = this.toCharArray();;
char v2[] = anotherString.toCharArray();
int i = offset;
int j = anotherString.offset;

if (i == j) {
int k = i;
int lim = n + i;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
} else {
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;
}

回答2:

必须用compareTo()吗
不是必须的话这样就行

import java.util.*;

public class TextString {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();

char[] ch = s.toCharArray();
Arrays.sort(ch);
System.out.println(ch);

}
}

回答3:

public class MyClass {
public void Sort(String[] data)
{
int l=data.length;
String tmp;
for(int i=0;i {
for(int j=i+1;j if(data[i].compareToIgnoreCase(data[j])<0) //注意此处是忽略大小写的比较
{
tmp=data[i];
data[i]=data[j];
data[j]=tmp;
}
System.out.println(data[i]);
}
}
}

回答4:

public static void getSort(String str){
char []chs = str.toCharArray();
Arrays.sort(chs);
System.out.println(chs);
}

回答5:

这个都能用那还怕什么啊?直接用一个char【】数组就可以了啊,冒泡法排序啊,最简单的。