【问题标题】:Converting string to char and sort in descending order (ascii)将字符串转换为字符并按降序排序(ascii)
【发布时间】:2012-10-09 13:04:37
【问题描述】:

我正在创建一个程序,它将使用户输入整数(一个接一个),存储在数组中并按降序显示整数。该程序还要求用户输入一个字符串,使用string.toCharArray() 将其转换为字符。我已经正确地将整数显示为降序。问题是我不知道如何降序显示字符。

这是整数的代码:

for(i=0;i<nums.length;i++){
        System.out.print("Enter value for index["+i+"] here --> ");
        nums[i]=Integer.parseInt(br.readLine());}

while(pass<nums.length){
        for(i=0;i<(nums.length-pass);i++){
            if(nums[i]<nums[i+1]){
                temp=nums[i];
                nums[i]=nums[i+1];
                nums[i+1]=temp;
            }
        }
        pass++;
}
System.out.println("\n\nThe numbers when sorted descendingly are :\n");
    for(i=0;i<nums.length;i++){
        System.out.println(nums[i]);

这是字符串到数组的代码。这是我遇到问题的地方。我在运行程序时没有错误,只是我不知道如何正确执行。

System.out.print("Input a string");
        String strValue= br.readLine();
        char[] chrValues;
        chrValues=strValue.toCharArray( );

while(flag<chrValues.length){
   for (c=0; c< (chrValues.length- flag); c++){
        if(chrValues[c]<chrValues[i+1]){
         tempo=chrValues[c];
                chrValues[c]=chrValues[c+1];
                chrValues[c+1]= tempo;}
   }
}
 flag++; 
 System.out.println("\n\nThe String when converted in character and sorted descendingly     are :\n");    
    for(c=0;i<chrValues.length;c++){
        System.out.println(chrValues[c]);}

顺便说一下,我用flag作为临时数组存储。

【问题讨论】:

    标签: java string char


    【解决方案1】:

    您已经有内置方法:-

        String str = "Rohit";
        char[] arr = str.toCharArray();
    
        System.out.println(arr);
        Arrays.sort(arr);   // Sort in Ascending order
        System.out.println(arr);
    

    对于降序,您可以定义一个Comparator 并将其传递给Arrays.sort() 方法..

    您可以使用Arrays#sortArrayUtils#toObjectDescending order..中进行排序。

    它的工作原理如下:-

        String str = "Rohit";
        char[] charArray = str.toCharArray();
    
        Character[] myCharArr = ArrayUtils.toObject(charArray);
    
        Arrays.sort(myCharArr, new Comparator<Character>() {
    
            @Override
            public int compare(Character char1, Character char2) {
                return char2.compareTo(char1);
            }
        });
    
        for (char val: myCharArr) {
            System.out.print(val);
        }
    

    【讨论】:

    • 嗨罗希特!感谢您的回复。我可以在排序中使用 If 语句吗?因为 "a" 在 ascii 代码中的 "A" 中是不同的。谢谢
    • @kix.. 那么,您希望它们被视为相同吗??
    • 您可以将它们按升序排序并向后打印。 ;)
    • @kix.. 或者您可以在调用 compareTo 之前将它们都转换为小写... 相应地编辑代码..
    • @rohit 不。我希望他们考虑不同。例如,我将输入“Abca”,输出应该是:a c b A。顺便说一句,我们不允许将它们更改为小写:(
    【解决方案2】:

    你可以试试

    String str = "AbCdEf";
    char[] arr = str.toCharArray();
    
    for(int i = 0; i < arr.length; i++) System.out.print(arr[i]);
    System.out.println();
    
    Arrays.sort(arr); // sorted in ascending order
    
    // print them backwards i.e. descending order.
    for(int i = arr.length - 1; i >= 0; i--) System.out.print(arr[i]);
    System.out.println();
    

    打印

    AbCdEf
    fdbECA
    

    【讨论】:

    • @PeterLawrey.. +1 好吧,有时,我们应该使用传统的 for 循环.. 不需要倒序排序.. 太好了 :)
    • @peter - 谢谢!将尝试此操作并稍后给您反馈。
    • @peter - 为什么我可能会丢失所需的精度:char found int on this code `for(int i = arr.length - 1; i >= 0; i--) System .out.print(arr[i]); System.out.println();
    • @kix 你的副本有问题,因为它为我编译和运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    相关资源
    最近更新 更多