Java Collections工具类

本文通过一个Java程序示例介绍了如何使用Java集合框架中的Collections工具类进行常见操作,包括生成随机整数列表、反转列表、排序、洗牌、查找最大最小值、计算元素频率、二分搜索及替换所有指定元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值