package myutil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Collections1 {
public static void main(String[] args){
ArrayList<Integer> nums=new ArrayList<Integer>();
Random random=new Random(System.currentTimeMillis());
for(int i=0;i<10;i++)
nums.add(random.nextInt(100));
System.out.println("original:\n"+nums);
Collections.reverse(nums);
System.out.println("reverse:\n"+nums);
Collections.sort(nums);
System.out.println("sort:\n"+nums);
Collections.shuffle(nums);
System.out.println("shuffle:\n"+nums);
System.out.println("max:\n"+Collections.max(nums));
System.out.println("min:\n"+Collections.min(nums));
System.out.println("frequency of "+nums.get(0)+":\n"+Collections.frequency(nums, nums.get(0)));
System.out.println("binarySearch of "+nums.get(4)+":\n"+Collections.binarySearch(nums, nums.get(4)));
int orginal=nums.get(2);
Collections.replaceAll(nums, orginal, 88);
System.out.println("replaceAll of "+orginal+" to value 88:\n"+nums);
}
}
Java Collections工具类
最新推荐文章于 2024-04-19 21:14:00 发布