• ArrayList


    java.util.Collection,Collection是java中保存元素的数据结构,因此数据结构采用java中集合collection。通过类型分类的接口来构造对数据结构分析。Collection类支持大多数集合类的操作

    Collection中包含不同数据结构的不同接口的共性,通过Collection类,自己定义属于自己的数据接口类的接口,下面ArrayList自己构造一个数组列表的类实现Collection接口,对Collection接口的理解

    存储一组类型相同的对象。

    一个类实现一个接口,如果其子类是非abstract类,必须要实现其中接口中的方法,若子类是abstract不一定要实现其中接口的方法

    1.ADT:抽象数据类型,抽象的数据类型可以add,remove,contain这样的操作,当然有并union和查找find,等操作,通过自己重写构造方法

    2.list按照位置存储元素的集合,一种动态的结构。在List接口中extends的Collection方法。

    3.数据结构与算法分析对数据结构一个升华,之前必须要先看看数据结构的表达方式,两者是明显分开的

    ArrayList 自己自定义的类,使用数据存储结构来阐述如何实现ArrayList类。ArrayList类实现List接口

    List接口中包含集合Collection接口所有方法,自己独特方法,List 接口中数据按照位置存储的动态结构

    ArrayList集合。下面采用数组的存储接口开发属于自己ArrayList集合,同时实现ArrayList集合中各种操作:

     ArrayList 最重要一个方法ensureCapacity() 对原来数组的开阔,这也是动态结构原因所在

     1 public class test {
     2     private int [] listArr;
     3     private int theSize;
     4     
     5     //构造方法初始化
     6     public test(int [] arr,int n)
     7     {
     8         this.theSize=n;
     9         this.listArr=arr;
    10     }
    11     public void ensureCapacity(int newCapacity)
    12     {
    13         if(newCapacity<theSize)
    14         {
    15             return;
    16         }
    17         //为原来的数组开辟新的长度空间  
    18         int []old=listArr;
    19         listArr= (int[])new int[newCapacity];
    20         
    21         for (int i=0;i<theSize;i++)
    22         {
    23             listArr[i]=old[i];
    24         }
    25     }
    26     
    27     public   void main1() {
    28         // TODO Auto-generated method stub
    29         
    30         System.out.println(theSize);
    31         ensureCapacity(8);
    32         System.out.println(listArr.length);
    33     }
    34     public static void main(String[] args)
    35     {
    36         int []arr={1,2,3,4,5};
    37         int n=5;
    38         test a=new test(arr,n);
    39         a.main1();
    40     }
    41 }

    注意点,1.构造方法初始化2.静态方法不能够直接使用非静态方法,间接调用:类对象名.方法,如上a.main1()

    自己定义编写的MyArrayList 与jdk中ArrayList的理解:

    package MyArrayList;
    
    import java.util.Iterator;
    
    //使用泛型类,实现Iterator的接口,必须实现Iterator中iterator方法
    public class MyArrayList <AnyType> implements Iterable<AnyType>{
        private static final int DEFAULT_CAPACITY=10;//ArryList中默认的容量10
        
        private int theSize;
        private AnyType [] theItems;//Number of elements
        public MyArrayList()
        {//构造函数初始化
            doClear();
        }
        
        private void doClear()
        {
            theSize=0;
            //开辟一个默认为10的数组
            enSureCapacity(DEFAULT_CAPACITY);
        }
        
        /*****为了外部调用内部方法****/
        public int size()
        {
            return theSize;
        }
        public boolean isEmpty()
        {
            return size()==0;
        }
        public void trimToSize()
        {
            enSureCapacity(size());
        }
        @SuppressWarnings("unchecked")
        public void enSureCapacity(int newCapacity)
        {
            if(newCapacity<theSize)
                return;
            //如果发现要的容量大于theSize,必须开辟更多空间
            AnyType [] old=theItems;
            theItems=(AnyType[]) new Object[newCapacity];
            for(int i=0;i<theSize;i++)
                theItems[i]=old[i];
            
        }
        public AnyType get(int idx)
        {
            if(idx<0||idx>=size())
                throw new ArrayIndexOutOfBoundsException();
            return theItems[idx];
        }
        public AnyType set(int idx,AnyType newVal)
        {
            if(idx<0||idx>=size())
                throw new ArrayIndexOutOfBoundsException();
            AnyType old=theItems[idx];
            theItems[idx]=newVal;
            return old;//告诉我们已经完成设置
        }
        public void add(int idx,AnyType x)
        {//防止是否在数组满情况下相加
            if(size()==theItems.length)
            {
                enSureCapacity(size()*2+1);
            }
            //在idx位置上插入一个数,将idx右侧数据向右边移动,腾出插入位置
            for(int i=theSize;i>idx;i--)
                theItems[i]=theItems[i-1];
            theItems[idx]=x;
            theSize++;
            
            
        }
        public AnyType remove(int idx)
        {
            //与插入相反,将右侧位置覆盖就行
            AnyType removeItem=theItems[idx];
            for(int i=idx;i<size();i++)
                theItems[i]=theItems[i++];
            theSize--;
            return removeItem;
        }
        //对ArrayList数组数进行遍历:
        public void access()
        {  AnyType val=null;
        
            for(int i=0;i<size();i++)
            {
                val=theItems[i];
                System.out.print(val+" ");
            }
        }
        //实现Iterator下iterator方法
        @Override
        public    java.util.Iterator<AnyType>  iterator() {
            // TODO Auto-generated method stub
            return new ArrayListIterator();
            //这个实列实现Iterator接口
        }
        private class ArrayListIterator implements java.util.Iterator<AnyType>
        {
            private int current=0;
            @Override
            public boolean hasNext() {
                // TODO Auto-generated method stub
                
                return current<size();
            }
    
            @Override
            public AnyType next() {
                // TODO Auto-generated method stub
                if(!hasNext())
                    throw new java.util.NoSuchElementException();
                return theItems[current++];
            }
            public void remove()
            {
                MyArrayList.this.remove(--current);
            }
            
        }
        public static  void main(String[] args)
        {
            @SuppressWarnings("unused")
            MyArrayList<Integer> test=new MyArrayList<Integer>();
            test.enSureCapacity(10);
            test.add(0, 1);
            test.access();
            
        }
    
        
    }
  • 相关阅读:
    搭建Maven版SSM工程
    mac终端常用的命令
    常见的HTTP请求错误
    Go通关03:控制结构,if、for、switch逻辑语句
    Go通关14:参数传递中,值、引用及指针之间的区别
    Go通关13:究竟在什么情况下才使用指针?
    Go通关12:如何写出高效的并发模式?
    Go通关11:并发控制神器之Context深入浅出
    Go通关10:并发控制,同步原语 sync 包
    Go通关09:并发掌握,goroutine和channel声明与使用!
  • 原文地址:https://www.cnblogs.com/woainifanfan/p/6002091.html
Copyright © 2020-2023  润新知