需要帮助排序ArrayList [英] Need help sorting ArrayList

查看:92
本文介绍了需要帮助排序ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何解除这一点赦免这句话



  private   void  sort(){

String []名称=( String [])members.toArray();
sortStringExchange(names);
for int k = 0 ; k < members.size(); k ++)
系统。 out 。 println(names [k]);
}

public static void sortStringExchange( String x [])
{
int i,j;
String temp;

for (i = 0 ; i < x.length - 1 ; i ++)
{
for (j = i + 1 ; j < x.length; j ++)
{
if (x [i] .compareToIgnoreCase(x [j])> 0
{ // 升序sort
temp = x [i];
x [i] = x [j]; // 交换
x [j] = temp;

}
}
}
}

解决方案

这个想法是使用自定义比较器,它应该是一些实现接口的类 Comparable< T> ,其中 T 应该是列表元素的类型(或者,可能是列表元素的运行时类型的公共基类型,抽象与否)。请参阅:

http://docs.oracle .com / javase / 6 / docs / api / java / lang / Comparable.html [ ^ ]。



您可以在这里找到一些代码示例: http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects -by-property [ ^ ]。



现在,这没有任何意义,也不会施放:

 字符串 [] names =( String  [])members.toArray(); 



在您的代码中,您应该比较表示元素的字符串,而不是列表或数组元素。相反,您应该使用 java.lang.Object.toString(),您可以在元素类中覆盖它。请参阅:

http://docs.oracle .com / javase / 7 / docs / api / java / lang / Object.html [ ^ ]。



-SA


试试这个



 私人  void  sort(){

String [] names =( String [])members.toArray();
// 删除此行
// sortStringExchange(names);
// 使用比较器添加此项,如Sergey所述
Arrays.sort(names, new Comparator< string>(){

@ Override
public int compare( String s1, String s2){

return s1.compareTo(s2);

}
});

for int k = 0 ; k< members.size(); k ++)
System.out.println(names [k]);
}





希望这会有所帮助:)



BCD


Can any one tell me how to sort this out pardon the pun

private void sort( ){

             String[] names = (String[]) members.toArray();
             sortStringExchange (names);
             for ( int k = 0;  k < members.size();  k++ )
            System.out.println(names[k] );
      }

      public static void sortStringExchange( String  x [ ] )
      {
            int i, j;
            String temp;

            for ( i = 0;  i < x.length - 1;  i++ )
            {
                for ( j = i + 1;  j < x.length;  j++ )
                {
                         if ( x [ i ].compareToIgnoreCase( x [ j ] ) > 0 )
                          {                                             // ascending sort
                                      temp = x [ i ];
                                      x [ i ] = x [ j ];    // swapping
                                      x [ j ] = temp;

                           }
                   }
             }
      }

解决方案

The idea is to use the custom comparator, which should be some class implementing the interface Comparable<T>, where T should be the the type of the element of your list (or, possibly, the common base type of the runtime types of the elements of the list, abstract or not). Please see:
http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html[^].

You will find some code sample here: http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property[^].

Now, this makes no sense and won't cast:

String[] names = (String[])members.toArray();


In your code, you should compare strings representing elements, not lists or arrays of elements. Instead, you should use java.lang.Object.toString() which you could override in your element class(es). Please see:
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html[^].

—SA


Try this

private void sort( ){

             String[] names = (String[]) members.toArray();
             // Remove this line
             //sortStringExchange (names);
             // add this using comparator as stated by Sergey
             Arrays.sort(names, new Comparator<string>() {

                @Override
                public int compare(String s1, String s2) {

                return s1.compareTo(s2);

           }
});
            
             for ( int k = 0;  k < members.size();  k++ )
              System.out.println(names[k] );
      }



Hope this helps :)

BCD


这篇关于需要帮助排序ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆