当前位置:   article > 正文

List排序及与String的转换_list 按一定顺序转string

list 按一定顺序转string

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class Teeee ...{
    List list = new LinkedList();
    public static void main(String[] args) ...{
        List list = new LinkedList();
        for (int i = 0; i < 9; i++) ...{
            list.add("a"+i);
        }
        Collections.sort(list);//顺序排列
        System.out.println(list);
       
        Collections.shuffle(list);//混乱的意思
        System.out.println(list);
       
        Collections.reverse(list);//倒序排列
        System.out.println(list);
       
        System.out.println(Collections.binarySearch(list, "a5"));//折半查找

        或者

       for(int i=list.size()-1;i>=0;i--)
       {
           System.out.println(list.get(i));
       }
   
}

  1. List<String> list = new ArrayList<String>();   
  2. list.add( "a1" );   
  3. list.add( "a2" );   
  4. String[] toBeStored = list.toArray( new String[list.size()]);   
  5. for (String s : toBeStored) {   
  6.       System.out.println(s);   
  7. }  
List<String> list = new ArrayList<String>(); list.add("a1"); list.add("a2"); String[] toBeStored = list.toArray(new String[list.size()]); for(String s : toBeStored) {      System.out.println(s); }
 
另外的一个例子:
  1. ArrayList<Point> a1 = new ArrayList<Point>();
  2.    a1.add(new Point(3, 3));
  3.    a1.add(new Point(4, 4));
  4.    a1.add(new Point(5, 5));
  1.    Object[] objs = a1.toArray(); // 利用ArrayList的toArray()返回一个对象的数组.
  2.    for (int i = 0; i < objs.length; i++)
  3.    {
  4.     System.out.println(objs[i]);
  5.    }
  6.    System.out.println(objs);//
  7.    List<Object> l = Arrays.asList(objs);// Arrays.asList()返回一个列表.
  8.    System.out.println(l);



  1. List<String> list = new ArrayList<String>();   
  2. list.add( "a1" );   
  3. list.add( "a2" );   
  4.            
  5. String[] toBeStored = new String[al.size()];   
  6. list.toArray(toBeStored);   
  7. for (String s : toBeStored) {   
  8.        System.out.println(s);   
  9. }  
List<String> list = new ArrayList<String>(); list.add("a1"); list.add("a2");          String[] toBeStored = new String[al.size()]; list.toArray(toBeStored); for (String s : toBeStored) {       System.out.println(s);

}

  1. List<String> list = new ArrayList<String>();   
  2. list.add( "a1" );   
  3. list.add( "a2" );   
  4.            
  5. String[] toBeStored = new String[]{};   
  6. String[] str = al.toArray(toBeStored);   
  7. for (String s : str) {   
  8.         System.out.println(s);   

List<String> list = new ArrayList<String>(); list.add("a1"); list.add("a2");          String[] toBeStored = new String[]{}; String[] str = al.toArray(toBeStored); for (String s : str) {        System.out.println(s); }



  1. String str[] = list.toArray( new String[]{}); 
  2. list.toArray()

http://hi.baidu.com/liangchaoyjs/blog/item/32aa89c72aaddad4d100609a.html

 

或者   list.toArray();最简单

附:将数组转换为list列表

public class Test
{
public static void main(String[] args)
{
String[] strs ={"a","b","c","d"};
List<String> list = Arrays.asList(strs);
Collections.shuffle(list);

for(String str:list)
{
System.out.println(str);
}
}
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/326234
推荐阅读
相关标签
  

闽ICP备14008679号