collections接口用法

import java.util.ArrayList;
import java.util.Collections;


public class ConllectionDemo {
	public static void main(String[] args) {
		ArrayList<Integer> arr = new ArrayList<Integer>();
		arr.add(1);
		arr.add(2);
		arr.add(3);
		arr.add(4);
		arr.add(5);
		arr.add(6);
		arr.add(0);
		
		//獲取集合中的最大值max,最小min
		//System.out.println(Collections.min(arr));
		
		//將列表中的元素翻轉
		//Collections.reverse(arr);
		//System.out.println(arr);
		
		//將列表中的元素亂序
		//Collections.shuffle(arr);
		//System.out.println(arr);
		
		//將列表中指定索引的元素交換
		//Collections.swap(arr, 2, 4);
		//System.out.println(arr);
		
		//將列表中的元素按自然順序升序排列
		Collections.sort(arr);
		System.out.println(arr);
		
		//將列表中的元素按自然順序降序排列(先升序sort 然後翻轉reverse)
		//Collections.sort(arr);
		//Collections.reverse(arr);
		//System.out.println(arr);
		
		//返回指定元素的索引 (binarySearch要求原來的List必須是升序排列的。)
		 System.out.println(Collections.binarySearch(arr,3));
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章