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

  • 相关阅读:
    小技巧-WEB API第一次加载很慢
    20165313 《Java程序设计》第一周学习总结
    spring-boot源码分析之BeanFactory · 叁
    spring-boot源码分析之BeanFactory · 贰
    aaa
    JavaScript FSO属性大全
    IE浏览器下用JS创建文件
    FileSystemObject详解
    HTTP状态码
    Spring中异步执行方法(@Async)
  • 原文地址:https://www.cnblogs.com/kide1412/p/10883047.html
Copyright © 2011-2022 走看看