• java ArrayList 底层详细


    package cn.com.test04;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    class MyArrayList<T>{
        private Object[] obj;
        private int size=0;
         MyArrayList(){
             this.obj= new Object[10];//this(10);
         }
         MyArrayList(int length){
            this.obj= new Object[length];
         }
         
         public void add(T o){
             automaticAdd();//判断数组是否需要增长
             this.obj[size]=o;
             size++;
         }
         public void add(int index,T o){
             automaticAdd();
              System.arraycopy(obj, index, obj, index+1, size-index);
             this.obj[index]=o;
             size++;
         }
         public T get(int index){
             if(index<0||index>=size){
                // System.err.print("位置不对");
                 throw new IndexOutOfBoundsException(
                            "Index: "+index+", Size: "+size);
             }
             return (T)obj[index];
         }
         public int size(){
             return this.size;
         }
         private void automaticAdd(){
             if(size>=obj.length*0.75){
                 Object[] o=new Object[(int) (obj.length*1.5)];
                 System.arraycopy(obj, 0, o, 0, size);
                 obj=o;
             }
         }
         public void update(int index,T o){
             if(index<0||index>=size){
                    // System.err.print("位置不对");
                     throw new IndexOutOfBoundsException(
                                "Index: "+index+", Size: "+size);
                 }
              obj[index]=o;
         }
         public T remove(int index){
             if(index<0||index>=size){
                 throw new IndexOutOfBoundsException(
                            "Index: "+index+", Size: "+size);
             }
             automaticRemove();
             T t=(T)obj[index];
             System.arraycopy(obj, index+1, obj, index, size-index-1);
             size--;
            return t;
             
         }
         private void automaticRemove(){
             if(size<=obj.length*0.7){
                 Object[] o=new Object[(int) (obj.length*0.75)];
                 System.arraycopy(obj, 0, o, 0, size);
                 obj=o;
             }
         }
         public int indexOf(T o){
             for(int i=0;i<size();i++){
                 if((T)obj[i]==o){
                     return i;
                 }
             }
             return -1;
         }
         public T[] toArray(){
             Object[] sss = Arrays.copyOf(obj, size);
            return (T[])sss;
             
         }
    }
    public class t03 {
    
        public static void main(String[] args) {
            ArrayList<String> l= new ArrayList<String>();// 底层用数组实现   查询 修改 方便   指定位置增加 删除效率低下
             l.add("aa");l.add("bbb"); l.add("aa");
             System.out.println(l.size());
             l.add("bbb");  l.add("aa");l.add("bbb"); l.add("aa");l.add(2,"aa");
            System.out.println(l.remove(1)+"==========="); ;
            System.out.println(l.indexOf("bbb")+"--------------");
            //l.update(0, "llllllllllllllll");
         Object[] sad = l.toArray();
           for(int i=0;i<sad.length;i++){
               System.out.println("++"+sad[i]);
           }
           // t03.show(l);
        }
          public static void show(MyArrayList l){
              for(int i=0;i<l.size();i++){
                  System.out.println(l.get(i));
              }
          }
    }
  • 相关阅读:
    butterknife异常提示:attribute value must be constant
    SharedPreferences第一次使用后HashMap将常驻内存
    js获取元素的innerText属性为什么为空
    针对focus和blur的Dom事件触发顺序
    android中View的GONE和INVISIBLE的原理
    HTML中div以及span等元素获取焦点
    android MotionEvent获得当前位置
    IE10 透明背景的div无法遮罩
    jquery中.attr('value')和.val()的区别
    __proto__和protaotype的区分
  • 原文地址:https://www.cnblogs.com/anholt/p/3655990.html
Copyright © 2020-2023  润新知