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

可用排序算法 compareTo()

第1个回答  2008-11-17
//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
* <code>String</code> object is compared lexicographically to the
* character sequence represented by the argument string. The result is
* a negative integer if this <code>String</code> object
* lexicographically precedes the argument string. The result is a
* positive integer if this <code>String</code> object lexicographically
* follows the argument string. The result is zero if the strings
* are equal; <code>compareTo</code> returns <code>0</code> exactly when
* the {@link #equals(Object)} method would return <code>true</code>.
* <p>
* 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 <i>k</i> be the smallest such index; then the string
* whose character at position <i>k</i> has the smaller value, as
* determined by using the < operator, lexicographically precedes the
* other string. In this case, <code>compareTo</code> returns the
* difference of the two character values at position <code>k</code> in
* the two string -- that is, the value:
* <blockquote><pre>
* this.charAt(k)-anotherString.charAt(k)
* </pre></blockquote>
* If there is no index position at which they differ, then the shorter
* string lexicographically precedes the longer string. In this case,
* <code>compareTo</code> returns the difference of the lengths of the
* strings -- that is, the value:
* <blockquote><pre>
* this.length()-anotherString.length()
* </pre></blockquote>
*
* @param anotherString the <code>String</code> to be compared.
* @return the value <code>0</code> if the argument string is equal to
* this string; a value less than <code>0</code> if this string
* is lexicographically less than the string argument; and a
* value greater than <code>0</code> 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个回答  推荐于2018-05-04
必须用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个回答  2008-11-17
public class MyClass {
public void Sort(String[] data)
{
int l=data.length;
String tmp;
for(int i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
if(data[i].compareToIgnoreCase(data[j])<0) //注意此处是忽略大小写的比较
{
tmp=data[i];
data[i]=data[j];
data[j]=tmp;
}
System.out.println(data[i]);
}
}
}
第4个回答  2008-11-18
public static void getSort(String str){
char []chs = str.toCharArray();
Arrays.sort(chs);
System.out.println(chs);
}
第5个回答  2008-11-17
这个都能用那还怕什么啊?直接用一个char【】数组就可以了啊,冒泡法排序啊,最简单的。

java编程:任意给出一个字符串数组,按照字母的顺序将其排序输出。
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 =...

编写Java程序,将一字符串其中的大小写字母的字符分别输出
循环字符串用charAt方法来获取字符,将字符转为int,只要判断转换后的数值就可以判断大小写字母,大写字母的范围是65-90,小写字母的范围是97-122。public class Main { public static void main(String[] args) { String s = "aaBvdEFdsd";StringBuffer ss = convertString(s);System.out.println(...

给一个有序的字符串怎么用java语言实现打乱顺序
取一个随机数,这个数就是你目标串的长度n 然后取n个随机数,取模到数组长度,然后合起来就完了...

JAVA通过数组按首字母排序怎么做?
自己封装的一个工具类,可以将汉字按照拼音的首字母排序,支持对Model和字典排序.只能对首字母排序,不支持第二个字母, 实现的原理就是创建一个A-Z的数组,数组中的元素是字典,字典中有两个元素,一个是title,就是当前的首字母 A-Z中的一个,另一个是保存当前title对应的元素的数组,对传入的数组进行遍...

java 字符串 字母表顺序排列
public class Test { public static void main(String args[]) { String[] strs = {"Apple","peach","banana","Pear"};for (int i = 0; i < strs.length-1; i++) { for (int j =i+1; j < strs.length; j++) { int intTemp = strs[i].compareToIgnoreCase(strs[j]);...

java 输入一个字符串,打印出该字符串中字符的所有排列
实现思路:就是输入字符串后,通过递归的方式,循环每个位置和其他位置的字符。import java.util.Scanner; public class Demo001 { public static void main(String[] args) { String str = ""; Scanner scan = new Scanner(System.in); str = scan.nextLine(); permutation(str.t...

JAVA通过数组按首字母排序
length; i++){ System.out.print(arrayToSort[i]+",");} } public static void main(String[] args) { new Test().sortStringArray();} } 运行结果:字符型数组排序,排序前:Oscar,Charlie,Ryan,Adam,David,aff,Aff,排序后:Adam,aff,Aff,Charlie,David,Oscar,Ryan,希望对你有帮助 ...

Java中,怎么打印出一个字符串的所有排列?
首先,创建一个名为printPermutations的函数,它接收一个字符数组作为输入。函数从索引index开始,通过递归实现排列生成。在每次迭代中,它会交换arr[index]与arr[index+1],然后递归处理arr从index+2到末尾的子数组。当index等于字符串长度减一,意味着完成了一个完整的排列,这时将当前arr转换为字符串并...

用java编写一个程序,实现字符串大小写的转换并倒序输出
public class Main {public static void main(String[] args) {String s = "aaBvdEFdsd";StringBuffer ss = convertString(s);System.out.println("大小写转换后是:" + convertString(s));System.out.println("倒序输出后是:" + ss.reverse());}public static StringBuffer convertString(...

java语言建立一个班级学生姓名的字符串数组并进行排序
import java.text.Collator;import java.util.Arrays;import java.util.Comparator;public class NameSortDemo {public static void main(String[] args) {String[] names = { "孙小美", "阿土伯", "小明", "钱夫人", "小红", "小菜" };System.out.println("排序前:");for (String name :...

相似回答