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("***************************************************");
      }
    }

  • 相关阅读:
    模块之使用模块
    函数之return
    Python3入门(八)——面向对象OOP
    Python3入门(七)——模块
    Python3入门(六)——函数式编程
    Python3入门(五)——高级特性
    Python3入门(四)——Python函数
    Python3入门(三)——Python基础语法
    Python3入门(二)——Python开发工具Pycharm安装与配置
    大数据入门第二十五天——elasticsearch入门
  • 原文地址:https://www.cnblogs.com/kide1412/p/10883047.html
Copyright © 2011-2022 走看看