zoukankan      html  css  js  c++  java
  • java基础笔记(9)----集合之list集合

    1. 集合
      1. 对于集合的理解,集合是一个容器,用于存储和管理其它对象的对象
    2. 集合,首先了解所有集合的父接口----collection
      1. 特点:存储任意object元素
      2. 方法
        1. boolean add(Object o) // 把元素o添加到集合中,成功true,否则false
        2. boolean addAll(Collection c) //把集合c中的所有元素 添加到当前集合中
        3. void clear() // 清空当前集合中的所有元素
        4. boolean contains(Object o) //判断对象o 在当前集合中是否存在
        5. boolean containsAll(Collection c) //判断集合c中的元素 在当前集合中是否都存在
        6. boolean isEmpty() // 判断当前集合中的元素个数是否为0
        7. boolean remove(Object o) // 把对象o 从当前集合中删除,返回是否成功
        8. int size() // 获取集合元素的实际个数
        9. Object[] toArray() // 把集合转成对应的数组
    3. list接口
      1. 特点:存储任意object元素,有序,有下标元素内容可以重
      2. 方法:
        1. 继承父接口Collection中的所有方法
        2. void add(int idx, Object obj) //在指定下标idx位置 插入元素obj
        3. Object get(int idx) // 获取指定下标idx位置上的元素
        4. int indexOf(Object o) // 返回o在当前集合中下标,如果不存在 返回-1
        5. Object remove(int idx) //删除指定下标idx位置的元素,并且返回该元素
        6. Object set(int idx,Object o) //修改idx位置的元素为o,并且返回修改前的元素
        7. List subList(int beginIdx, int toIdx)//截取子集合,从下标beginIdx(含)到toIdx(不含)
      3. 遍历:下标遍历,forEach遍历,迭代遍历
        1. 如下:

    package com.lvsling.test;

    import java.util.ArrayList;

    import java.util.Iterator;

    import java.util.List;

    public class TestArrayList {

        public static void main(String[] args) {

            //student--有三个属性,nameageclazz

            List<Student> list = new ArrayList<Student>();

            

            Student s1 = new Student("jerry",20,"1");

            list.add(s1);

            list.add(new Student("tom",18,"2"));

            list.add(new Student("mike",15,"1"));

            

            // 下标遍历

            for(int i=0; i<list.size(); i++){

                System.out.println(list.get(i));

            }

            System.out.println("-------------------------");

            

            // forEach遍历

            for(Object obj : list){

                Student s = (Student)obj;

                System.out.println(s.getName());

            }

            System.out.println("-------------------------");

            

            // 迭代遍历

            Iterator it = list.iterator(); // 获取集合对象的迭代器

            while(it.hasNext()){ // 判断是否有下一元素

                Object obj = it.next(); // 获取下一元素

                System.out.println(obj);

            }

        }

    }

     

    1. list实现类
      1. ArrayList
        1. 数组实现,可变长数组
        2. 线程不安全,效率高
        3. 查询快,增删慢
      2. Vector
        1. 数组实现,可变长数组
        2. 线程安全,效率高
      3. LinkedList
        1. 链表实现
        2. 查询慢,增删快
    1. 泛型集合
      1. 类型安全的集合,限制集合元素的类型,必须是相同的。
        1. 泛型类型,前后一致
        2. 泛型类型必须是引用类型(基本类型使用包装类)
    2. 拓展与提升
      1. 工具类
        1. Collections类,是工具类,提供了一组static方法,用于 对集合进行操作。
        2. 如:
          1. Collections.sort(list) // 按升序排----注意:集合元素类型 必须实现java.lang.Comaparable接口。
          2. Collections.reverse(list); // 集合元素反转
          3. Collections.shuffle(list); // 随机显示集合元素
  • 相关阅读:
    基于term vector深入探查数据
    oracle 行转列
    oracle 统计成绩
    最全最新个税计算公式---今天你税了吗?
    .net反混淆脱壳工具de4dot的使用
    使用ILSpy软件反编译.Net应用程序的方法及注意事项
    EmguCV使用Stitcher类来拼接图像
    从睡姿就可以看出你的性格,据说非常准,快存!
    分享几个.NET WinForm开源组件,纪念逐渐远去的WinForm。。。
    【转载】关于.NET下开源及商业图像处理(PSD)组件
  • 原文地址:https://www.cnblogs.com/lvsling/p/8463694.html
Copyright © 2011-2022 走看看