【问题标题】:How can I sort a string array in ascending order, by number of vowels in each string?如何按每个字符串中的元音数量按升序对字符串数组进行排序?
【发布时间】:2017-05-25 01:57:57
【问题描述】:
    import java.util.*;
    class VowelAsc
    {
        public static void main(String args[])
        {
            int count=0;
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            String [] s=new String[n];
            int [] b=new int[40];
            for(int i=0;i<n;i++)
            { 
                s[i]=sc.next();
            }
            for(int i=0;i<s.length;i++)
            {
                char[] a=s[i].toCharArray();
                for(int c=0;c<a.length;c++)
                {

                    if(a[c]=='a' || a[c]=='e' || a[c]=='i' || a[c]=='o' || a[c]=='u' ||a[c]=='A' ||a[c]=='E' || a[c]=='I' || a[c]=='O' || a[c]=='U')
                    {
                       count++;
                       //b[c]=count;
                    }
                }
                if(count>0)
                {
                    if(i<s.length)
                    {
                        String t=s[i];
                        s[i]=s[i+1];
                        s[i+1]=t;
                    }
                } 
           }
      }
 }

我正在尝试计算每个字符串中存在的元音,并且我想根据我无法做到的计数变量交换字符串。 接受字符串后,我使用 toCharArray() 函数将其转换为 char 数组,并将每个字符与大小写元音进行比较。

我收到一个错误。在编写代码部分的任何帮助将不胜感激。

输入:

n=4
xyz
bad
aeiou
hello

输出:

aeiou
hello
bad
xyz

【问题讨论】:

  • 你的问题/问题是?
  • 错误是什么?
  • “我收到一个错误” - 哪个错误?
  • 如何根据每个字符串中存在的元音数量以升序交换字符串数组中的字符串。
  • "ArrayIndexOutOfBoundsException" 可能是由 "s[i+1]" 当 i=(s.length-1) 引起的。

标签: java string sorting indexoutofboundsexception


【解决方案1】:

嗯...这可能略高于顶部(List 和 RegEx),但如果您不必执行数百万次,那么通过自定义 Comparator 将它们排序到列表中就可以了:

String[] s = new String[]{"xyz", "bad", "aeiou", "hello"};

Arrays.sort(s, new Comparator<String>(){
    @Override
    public int compare(String o1, String o2) {
        return o2.replaceAll("[^aeiouAEIOU]", "").length()
                - o1.replaceAll("[^aeiouAEIOU]", "").length();
    }
});

编辑:通过删除列表进行优化,感谢@Holger

【讨论】:

  • 能否请您解释一下字符串 o1 和 o2。
  • @mahesh 他们是方法参数compare(String o1, String o2)
  • 他正在使用一个比较器函数,该函数有效地遍历值集合中的所有实例,并找出两者中哪一个具有最低/最高值。尝试使用谷歌搜索冒泡排序,您将了解它的工作原理。虽然这是一个稍微高级的答案,但它是一个非常好的答案。
  • @mahesh Comparator 的 compare() 方法决定第一个或第二个给定字符串(o1 或 o2)在排序顺序中是否“更大”。在这种情况下,两个字符串都被缩减为只有它们的元音,然后比较它们的长度。如果 o1 小于 o2,compare() 给出负整数,所以我们只需从 o2 的长度中减去 o1 的长度。
  • 不需要最后的sList.toArray(s); 调用。您已经对数组进行了排序,因为 Arrays.asList(s) 返回了数组周围的包装器。 sList 的每次修改都会修改底层的 s 数组。当然,当你只使用Arrays.sort(s, your comparator)时,你甚至不需要Arrays.asList...
【解决方案2】:

类似于 mumpitz 代码,但针对性能进行了优化:

class Comp implements Comparator<String> {
  private static boolean[] isVowel = new boolean[127];
  static {
    isVowel['a'] = true;
    isVowel['e'] = true;
    isVowel['i'] = true;
    isVowel['o'] = true;
    isVowel['u'] = true;
  }
  @Override

  public int compare(String o1, String o2) {
    return count(o1) - count(o2); 
  }

  private int count(String s) {
    int cnt = 0;
    for (int i=0; i<s.length(); i++) {
      char c = s.charAt(i);
      if (c < 128 && isVowel(c))
        cnt++;
      }
    }
    return cnt;
  }
}

String[] s = new String[]{"xyz", "bad", "aeiou", "hello"};
List<String> sList = Arrays.asList(s);

Collections.sort(sList, new Comp());
s = sList.toArray(s);

【讨论】:

  • “类似于 mumpitz 代码,”调用 sList.toArray(s); 已过时。见this comment
猜你喜欢
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 2018-06-06
  • 2012-04-03
  • 1970-01-01
  • 2014-04-23
  • 2014-10-28
  • 1970-01-01
相关资源
最近更新 更多