zoukankan      html  css  js  c++  java
  • 集合综合练习

    package cn.kgc.demo03collections;

    import java.util.*;

    public class Demo02Sort {
      public static void main(String[] args) {
        // 1 创建集合
        ArrayList al = new ArrayList();
        // 2 添加数据对象
        Collections.addAll(al,new Person("张三",18),new Person("李四",16),new Person("王五",25),new Person("赵二",17));
        // 3 遍历显示:增强for
        for(Object per:al){
          System.out.println(per);
        }
        // 分割线
        System.out.println("***************************************************");
        // 4 遍历显示:迭代器
        Iterator iter = al.iterator();
        while(iter.hasNext()){
          Person person =(Person)iter.next();
          System.out.println(person);
        }
        // 5 取第二个数据
        System.out.println("***************************************************");
        System.out.println(al.get(1));
        // 6 年龄升序排列
        System.out.println("***************************************************");
        Collections.sort(al, new Comparator<Person>() {
          @Override
          public int compare(Person o1, Person o2) { return o1.getAge()-o2.getAge(); }
          });
        System.out.println(al);
        // 7 打乱顺序
        System.out.println("***************************************************");
        Collections.shuffle(al);
        System.out.println(al);
        // 8 年龄大到小排列
        System.out.println("***************************************************");
        Collections.sort(al, new Comparator<Person>() {
          @Override
          public int compare(Person o1, Person o2) { return o2.getAge()-o1.getAge(); }
          });
        System.out.println(al);
        // 9 删除第三个
        System.out.println("***************************************************");
        al.remove(2);
        System.out.println(al);
        // 10 清空集合
        System.out.println("***************************************************");
        al.clear();
        System.out.println(al);
        System.out.println("***************************************************");
      }
    }

  • 相关阅读:
    微信坚硬的后脚跟
    [项目整理]Win32,MFC的可执行文件只能运行一次
    美司法部索要维基解密志愿者谷歌账户内容
    QML性能
    OSGi 的核心配置、动态化及问题
    OSGi 的由来和本质特性
    机器视觉与计算机视觉
    人工智能与深度学习
    活着就能改变世界
    选择与执行
  • 原文地址:https://www.cnblogs.com/kide1412/p/10883047.html
Copyright © 2011-2022 走看看