zoukankan      html  css  js  c++  java
  • Day16_ArrayList的使用

    ArrayList的使用

    • 迭代器:专门用来遍历集合一种方式

      • hasNext();有下一个元素吗?如果有怎返回true,否则false
      • next();获取下一个元素
      • remove();删除元素
    • List接口的特点:有序有下标,可以重复

    • List常见实现类

      • ArrayList:

        源码分析:默认容量DEFAULT_CAPACITY = 10

        ​ 注意:如果没有向几个中添加任何元素时,容量0,添加任何一个数据之后,容量是10

        ​ 每次扩容原来的1,5倍

        elementDate存放元素数组

        size 实际元素个数

        public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
            return true;
        }
        
        private void ensureCapacityInternal(int minCapacity) {
            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
                    minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
            }
            ensureExplicitCapacity(minCapacity);
        }
        
        private void ensureExplicitCapacity(int minCapacity) {
            modCount++;
        
            // overflow-conscious code
            if (minCapacity - elementData.length > 0)
                grow(minCapacity);
        }
        
        private void grow(int minCapacity) {
            // overflow-conscious code
            int oldCapacity = elementData.length;
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            if (newCapacity - MAX_ARRAY_SIZE > 0)
                newCapacity = hugeCapacity(minCapacity);
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
        }
        
      • Vector:

        • 数组结构实现,查询快、增删慢
        • JDK1.0版本,运行效率慢、线程安全
        package com.oop.demo13;
        
        import java.util.Enumeration;
        import java.util.Vector;
        
        public class Demo01 {
            public static void main(String[] args) {
                //创建集合
                Vector vector=new Vector <>();
                //添加元素
                vector.add ("apple");
                vector.add ("lemon");
                vector.add ("pear");
                vector.add ("water");
                System.out.println ("元素个数"+vector.size ());
                //2.删除
                vector.remove ("pear");
                System.out.println ("删除之后"+vector.size ());
                //vector.clear ();
                //4.判断
                System.out.println (vector.contains ("lemon"));
                System.out.println (vector.isEmpty ());
                //3.遍历
                //使用枚举器
                System.out.println ("--------使用枚举器----------");
                Enumeration en = vector.elements ();
                while (en.hasMoreElements ()){
                    String o = (String)en.nextElement ();
                    System.out.println (o);
                }
                //5.vector其他方法
                System.out.println ("-----------------");
                System.out.println (vector.firstElement ());//得到第一个元素
                System.out.println (vector.lastElement ());//得到最后一个元素
                System.out.println ( vector.elementAt (1));//获取某个位置的元素
            }
        }
        
      • L.inkedList:

        • 链表结构实现,增删快,查询慢。
    package com.oop.demo13;
    
    import com.oop.Demo12.Student;
    
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.ListIterator;
    
    /**
     * LinkedList使用
     * 存储结构:双向链表
     */
    public class Demo02 {
        public static void main(String[] args) {
            //创建集合
            LinkedList linkedList=new LinkedList ();
            //添加元素
            Student s1=new Student ("xiaoming",10);
            Student s2=new Student ("xiaoHong",16);
            Student s3=new Student ("xiaobai",14);
            linkedList.add (s1);
            linkedList.add (s2);
            linkedList.add (s3);
            //linkedList.add (s1);//可重复元素个数4
            //[Student[name='xiaoming', age=10], Student[name='xiaoHong', age=16], Student[name='xiaobai', age=14], Student[name='xiaoming', age=10]]
            System.out.println ("元素个数"+linkedList.size ());
            System.out.println (linkedList.toString ());
            //2.删除
            /**Student类
             * @Override
             *     public boolean equals(Object obj) {
             *         //1判断是不是同一个对象
             *         if(this==obj){
             *             return true;
             *         }
             *         //2.判断是否为空
             *         if (obj==null) {
             *             return false;
             *         }
             *         //3.判断是否是Student类型
             *         if (obj instanceof Student){
             *             Student s=(Student) obj;
             *             //比较属性
             *             if (this.name.equals (s.getName ())&&this.age==s.getAge ()){
             *                 return true;
             *             }
             *
             *         }
             *         //5不满足条件返回false
             *         return false;
             *     }
             */
            /*linkedList.remove (new Student ("xiaoming",10));
            System.out.println ("删除之后"+linkedList.size ());//删除之后2
            linkedList.clear ();*/
            //3.遍历
            //3.1for遍历
            System.out.println ("-----3.1for遍历-------");
            for (int i = 0; i < linkedList.size (); i++) {
                System.out.println (linkedList.get (i));
            }
            System.out.println ("-----3.2增强for遍历------");
            for (Object o : linkedList) {
                Student s=(Student)o;
                System.out.println (s.toString ());
            }
            //3.3使用迭代器
            System.out.println ("-------3.3使用迭代器-----");
            Iterator it=linkedList.iterator ();
            while (it.hasNext ()){
                Student s=(Student)it.next ();
                System.out.println (s.toString ());
            }
            //3.4使用列表迭代器
            System.out.println ("-------3.4使用列表迭代器---");
            ListIterator lit =linkedList.listIterator ();
            while (lit.hasNext ()){
                Student s= (Student) lit.next ();
                System.out.println (s.toString ());
            }
    
            //判断
            System.out.println (linkedList.contains (s1));
            System.out.println (linkedList.isEmpty ());
            //5.获取
            System.out.println (linkedList.indexOf (s1));
        }
    }
    
  • 相关阅读:
    myeclipse导入项目中的乱码问题的解决
    myeclipse中的jar包的引入与新建
    myeclipse如何修改默认存储文件路径
    oracle迁移数据到mysql
    如何设置myeclipse的编码格式
    tns的查找与修改
    在PL/SQL中输入SQL语句时关键字的首字母自动变成大写
    滤器处理中文编码
    题解导航
    莫队总结应用
  • 原文地址:https://www.cnblogs.com/lemonlover/p/14074405.html
Copyright © 2011-2022 走看看