|NO.Z.00062|——————————|BigDataEnd|——|Java&集合类库.V10|——|Java.v09|Collections集合|

一、Collections类:[Collections类的编程使用]
### --- 基本概念

——>        java.util.Collections类主要提供了对集合操作或者返回集合的静态方法。
二、常用的方法
方法声明 功能介绍
static <T extends Object & Comparable<? super T>>
Tmax(Collection<? extends T> coll)
根据元素的自然顺序返回给定集合的
最大元素
static T max(Collection<? extends T> coll,
Comparator<?super T> comp)
根据指定比较器引发的顺序返回给定集合的
最大元素
static <T extends Object & Comparable
<?super T>> Tmin(Collection<? extends T> coll)
根据元素的自然顺序返回给定集合的
最小元素
static T min(Collection<? extends T> coll,
Comparator<?super T> comp)
根据指定比较器引发的顺序返回给定集合的
最小元素
static void copy(List<? super T> dest,
List<? extends T>src)
将一个列表中的所有元素复制到另一个
列表中
方法声明 功能介绍
static void reverse(List<?> list) 反转指定列表中元素的顺序
static void shuffle(List<?> list) 使用默认的随机源随机置换指定的列表
static <T extends Comparable<? super T>> voidsort(List list) 根据其元素的自然顺序将指定
列表按升序排序
static void sort(List list, Comparator<? super T> c) 根据指定比较器指定的顺序对
指定列表进行排序
static void swap(List<?> list, int i, int j) 交换指定列表中指定位置的元素
二、编程代码
package com.yanqi.task10;

/**
 * 实现静态内部类的定义和使用
 */
public class StaticOuter {
    private int cnt = 1;        // 隶属于对象层级
    private static int snt = 2; // 隶属于类层级

    public /*static*/ void show() {
        System.out.println("外部类的show方法就是这里!");
    }

    /**
     * 定义静态内部类   有static关键字修饰隶属于类层级
     */
    public static class StaticInner {
        private int ia = 3;
        private static int snt = 4;

        public StaticInner() {
            System.out.println("静态内部类的构造方法哦!");
        }

        public void show() {
            System.out.println("ia = " + ia); // 3
            System.out.println("外部类中的snt = " + snt); // 2
            //System.out.println("外部类的cnt = " + cnt); // Error:静态上下文中不能访问非静态的成员,因此此时可能还没有创建对象
        }

        public void show2(int snt) {  // 就近原则
            System.out.println("snt = " + snt); // 5
            System.out.println("内部类中的成员snt = " + StaticInner.snt); // 4
            System.out.println("外部类中的成员snt = " + StaticOuter.snt); // 2
            //StaticOuter.show();
            new StaticOuter().show();
        }
    }
}
三、编程代码
package com.yanqi.task15;

import com.yanqi.task10.StaticOuter;

import java.util.*;

public class CollectionsTest {

    public static void main(String[] args) {

        // 1.准备一个集合并初始化
        List<Integer> lt1 = Arrays.asList(10, 30, 20, 50, 45);
        // 2.实现集合中元素的各种操作
        System.out.println("集合中的最大值是:" + Collections.max(lt1)); // 50
        System.out.println("集合中的最小值是:" + Collections.min(lt1)); // 10

        // 实现集合中元素的反转
        Collections.reverse(lt1);
        System.out.println("lt1 = " + lt1); // [45, 50, 20, 30, 10]
        // 实现两个元素的交换
        Collections.swap(lt1, 0, 4);
        System.out.println("交换后:lt1 = " + lt1); // [10, 50, 20, 30, 45]
        // 实现元素的排序
        Collections.sort(lt1);
        System.out.println("排序后:lt1 = " + lt1); // [10, 20, 30, 45, 50]
        // 随机置换
        Collections.shuffle(lt1);
        System.out.println("随机置换后:lt1 = " + lt1); // [30, 10, 45, 20, 50] 随机
        // 实现集合间元素的拷贝
        //List<Integer> lt2 = new ArrayList<>(20);
        List<Integer> lt2 = Arrays.asList(new Integer[10]);
        System.out.println("lt1的大小是:" + lt1.size());
        System.out.println("lt2的大小是:" + lt2.size());
        // 表示将lt1中的元素拷贝到lt2中
        Collections.copy(lt2, lt1);
        System.out.println("lt2 = " + lt2);
    }
}
四、 编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=65158:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task15.CollectionsTest
集合中的最大值是:50
集合中的最小值是:10
lt1 = [45, 50, 20, 30, 10]
交换后:lt1 = [10, 50, 20, 30, 45]
排序后:lt1 = [10, 20, 30, 45, 50]
随机置换后:lt1 = [20, 45, 10, 50, 30]
lt1的大小是:5
lt2的大小是:10
lt2 = [20, 45, 10, 50, 30, null, null, null, null, null]

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(16)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示