Arrays排序

一,数组排序

      1. 数字排序  int[] intArray = new int[] { 4, 1, 3, -23 };

  Arrays.sort(intArray);

  输出: [-23, 1, 3, 4]

  2. 字符串排序,先大写后小写 String[] strArray = new String[] { "z", "a", "C" };

  Arrays.sort(strArray);

  输出: [C, a, z]

  3. 严格按字母表顺序排序,也就是忽略大小写排序 Case-insensitive sort

  Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);

  输出: [a, C, z]

  4. 反向排序, Reverse-order sort

  Arrays.sort(strArray, Collections.reverseOrder());

  输出:[z, a, C]

  5. 忽略大小写反向排序 Case-insensitive reverse-order sort

  Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);

  Collections.reverse(Arrays.asList(strArray));

  输出: [z, C, a]

二,数组和列表互转

   

         String[] str = new String[]{"a", "b", "c", "d"};
		List<String> st = Arrays.asList(str);             //数组转list
		System.out.println(str);
		
		String[] b = st.toArray(new String[st.size()]);   //list转数组
		System.out.println(b);   

三,List的双向遍历

        //创建list
        List<Integer> list = new ArrayList<>();
        for(int i = 1; i < 5; i ++){
            list.add(i);
        }
        
        //反向遍历
        ListIterator<Integer> listIterator = list.listIterator();
        while(listIterator.hasPrevious()){
            System.out.println(listIterator.previous());
        }
        
        //正向遍历
        while(listIterator.hasNext()){
            System.out.println(listIterator.next());
        }

 四,数组拷贝

     String[] strArray = new String[] { "ab", "xy", "mn" };  
        String[] copyArray = new String[4]; 
     //系统拷贝 System.arraycopy(strArray, 0, copyArray, 0, strArray.length); //Arrays拷贝 String[] copyArray1 = Arrays.copyOf(strArray, strArray.length);

 

  

  

posted @ 2017-03-31 11:53  wanhua.wu  阅读(351)  评论(0编辑  收藏  举报