java模拟实现ArrayList

默认初始化数组长度为5,扩容时为10。直接上代码

  1 package com.bjpowernode.test.datastructure;
  2 
  3 public class MyArraylist {
  4 
  5     private Object [] datas;
  6 
  7     /**
  8      * 初始化数组大小
  9      */
 10     private static int initsize = 5;
 11 
 12     int index = 0;//数组下标从0开始到size - 1
 13 
 14     public MyArraylist() {
 15         datas = new Object[initsize];
 16     }
 17 
 18     /**
 19      * 默认放在最后一个位置
 20      * @param obj - 目标元素
 21      */
 22     public void add(Object obj) {
 23         int length = datas.length;
 24         if (index > length - 1) {
 25            // throw new RuntimeException("满了,扩容去吧");
 26             //扩容
 27             copyArr();
 28         }
 29         datas[index] = obj;
 30         index++;
 31     }
 32 
 33     /**
 34      * 把元素放在指定位置
 35      * @param obj - 目标元素
 36      * @param _index - 指定下标位置
 37      */
 38     public void add(Object obj, int _index) {
 39         if (index >= datas.length) {
 40           //  throw new RuntimeException("数组已满");
 41             //扩容
 42             copyArr();
 43         }
 44         if (_index > datas.length - 1 || _index < 0) {
 45             throw new RuntimeException("数组越界");
 46         }
 47         if (_index == datas.length - 1) {
 48                 //是要放入数组的末尾,不涉及移动后面元素的位置
 49                 datas[_index] = obj;
 50         } else {
 51             //插入数组非尾的位置
 52             for (int i = index - 1; i >=  _index; i--) {
 53                 datas[i + 1 ] = datas[i];
 54             }
 55             datas[_index] = obj;
 56         }
 57         index++;
 58     }
 59     public Object get(int _index) {
 60         if (_index > datas.length - 1 || _index < 0) {
 61             throw new RuntimeException("数组越界");
 62         }
 63         return datas[_index];
 64 
 65     }
 66 
 67     /**
 68      * 移除指定元素
 69      * @param obj - 目标元素
 70      * @return
 71      */
 72     public boolean remove(Object obj) {
 73         for (int i = 0; i < datas.length; i++) {
 74             if (obj == null) {
 75                 if (equals(datas[i] == null)) {
 76                     //后面的元素向前移动,最后一个元素给空
 77                     for (int j = i + 1; j < index ; j++) {
 78                         datas[j - 1] = datas[j];
 79                     }
 80                     datas[index - 1] = null;
 81                     break;
 82                 }
 83             } else {
 84                 if (obj.equals(datas[i])) {
 85                     //后面的元素向前移动,最后一个元素给空
 86                     for (int j = i + 1; j < index ; j++) {
 87                         datas[j - 1] = datas[j];
 88                     }
 89                     datas[index - 1] = null;
 90                     break;
 91                 }
 92             }
 93 
 94         }
 95         index--;
 96         return true;
 97     }
 98 
 99     /**
100      * 移除指定位置的元素
101      * @param obj - 目标元素
102      * @param _index - 指定下标位置
103      * @return
104      */
105     public boolean remove(Object obj, int _index) {
106         if (_index > datas.length - 1 || _index < 0) {
107             throw new RuntimeException("数组越界");
108         }
109         for (int j = _index + 1; j < index; j++) {
110             datas[j - 1] = datas[j];
111         }
112         datas[index - 1] = null;
113         index--;
114         return true;
115     }
116 
117     /**
118      * 扩容
119      */
120     void copyArr() {
121         Object [] tmp = new Object[initsize * 2];
122         for (int i= 0; i < datas.length; i++) {
123             tmp[i] = datas[i];
124         }
125         datas = tmp;
126     }
127 }
128 class Test {
129 
130     public static void main(String[] args) throws Exception {
131         MyArraylist myArraylist = new MyArraylist();
132         myArraylist.add("a");
133         myArraylist.add("b");
134         myArraylist.add("c");
135         myArraylist.add("d");
136         myArraylist.add("e");
137         myArraylist.get(2);
138         myArraylist.add("f", 2);
139         myArraylist.remove("f", 2);
140 
141     }
142 }

猜你喜欢

转载自www.cnblogs.com/hkdpp/p/11813143.html