• 结合源码手写ArrayList


    1.项目结构

     2.定义接口

     1 package list;
     2 
     3 public interface MyList<E> {
     4     public abstract void add(E object);
     5 
     6     public abstract void add(int index, E object);
     7 
     8     public abstract boolean remove(E object);
     9 
    10     public abstract Object remove(int index);
    11 
    12     public abstract int getSize();
    13 
    14     public abstract Object get(int index);
    15 
    16     public abstract void set(int index, Object object);
    17 }
    1. 3.定义实现类
     1 package list;
     2 
     3 import java.util.Arrays;
     4 
     5 public class MyArrayList<E> implements MyList {
        //定义数组
    6 private transient Object[] elementData;
        //元素个数
    7 private int size; 8   //构造方法初始化数组容量 9 public MyArrayList() { 10 this(10); 11 } 12     13 public MyArrayList(int initialCapacity) { 14 if (initialCapacity < 0) { 15 throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); 16 } 17 elementData = new Object[initialCapacity]; 18 } 19 20 @Override 21 public void add(Object object) { 22 ensurExplicitVapacity(size + 1); 23 elementData[size++] = object; 24 } 25 26 //数组扩容 27 private void ensurExplicitVapacity(int minCapacity) { 28 if (minCapacity > elementData.length) { 29 int oldCapacity = elementData.length; 30 int newCapacity = oldCapacity + (oldCapacity >> 1); 31 elementData = Arrays.copyOf(elementData, newCapacity); 32 } 33 } 34 35 @Override 36 public void add(int index, Object object) { 37 rangeCheck(index); 38 ensurExplicitVapacity(size + 1); 39 System.arraycopy(elementData, index, elementData, index + 1, size); 40 elementData[index] = object; 41 size++; 42 } 43 44 //检测数组是否索引越界 45 private void rangeCheck(int index) { 46 if (index >= size || index < 0) { 47 throw new IndexOutOfBoundsException("lenth-->" + index); 48 } 49 } 50 51 @Override 52 public boolean remove(Object object) { 53 for (int i = 0; i < elementData.length; i++) { 54 Object element = elementData[i]; 55 if (element.equals(object)) { 56 remove(i); 57 return true; 58 } 59 } 60 return false; 61 62 } 63 64 @Override 65 public Object remove(int index) { 66 Object object = get(index); 67 int numMoved = elementData.length - index - 1; 68 if (numMoved > 0) { 69 System.arraycopy(elementData, index + 1, elementData, index, numMoved); 70 } 71 elementData[--size] = null; 72 return object; 73 } 74 75 @Override 76 public int getSize() { 77 return size; 78 } 79 80 @Override 81 public Object get(int index) { 82 rangeCheck(index); 83 return elementData[index]; 84 } 85 86 @Override 87 public void set(int index, Object object) { 88 rangeCheck(index); 89 remove(index); 90 E e = (E) object; 91 add(index, e); 92 } 93 }

    4.定义测试类

    package list;
    
    import java.util.ArrayList;
    
    public class Test {
        public static void main(String[] args) {
            MyArrayList<String> list = new MyArrayList<>();
            for (int i = 0; i < 100; i++) {
                list.add(i + "");
            }
            list.add(0, "按照索引添加元素");
            list.add(0, "fff");
            printList(list);
            //list.remove("fff");
            //list.remove(0);
            list.set(0,"set");
            printList(list);
    
        }
       //遍历打印
        public static void printList(MyArrayList<String> list) {
            System.out.print("[");
            for (int i = 0; i < list.getSize(); i++) {
                if (i != list.getSize()) {
                    System.out.print(list.get(i) + ", ");
                } else {
                    System.out.print(list.get(i) + "]");
                }
            }
        }
    }

    5.总结

      Arraylist源码难点 : 数组是如何扩容的

      这里只是写了一些Arraylist的简单逻辑, 还有很多细节没有去实现,编写这段代码只是为了更好的理解ArrayList的实现原理

      立志终生学习

      原文来源: https://blog.csdn.net/sonwing_for/article/details/81751072

  • 相关阅读:
    烟大课表PC端-不仅仅是浏览器和手机APP
    关于51单片机电子时钟精度的问题
    第十二周项目4-点、圆的关系
    Git on Windows 一些问题
    vi 的使用
    Git 账户认证的一些问题
    [Windows] win7 配置Java开发环境
    Velocity 局部定制模板
    [Storm] Storm与asm的恩恩怨怨
    [Storm] No data flows into bolt
  • 原文地址:https://www.cnblogs.com/yanjy/p/14023904.html
Copyright © 2020-2023  润新知