1.自定义ArrayList类功能
1)添加元素:public void add(Item obj);
2)根据索引删除某个元素:public Item remove(int index)
3)删除ArrayList中的某个元素:public boolean remove(Item item)
4)根据索引获取某个元素:public Item get(int index)
5)实现迭代器,可以遍历列表
6)获取列表大小:public int size()
7)判断列表是否为空:public boolean isEmpty()
2.代码实现:
class myArrayList<Item> implements Iterable<Item> { private Item[] elementData; private int size; public myArrayList() { this(10); //默认的情况下,大小为10 } public myArrayList(int initialCapacity) { if(initialCapacity<0) //如果列表大小设置小于0,会报出异常 { try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } } elementData = (Item[])new Object[initialCapacity]; //没有泛型数组,所以这里使用强制转换 } //列表大小 public int size() { return size; } //判断是否为空 public boolean isEmpty() { return size==0; } //增加元素 public void add(Item obj) { //数组扩容 /* 数组扩容的基本方法,将size*2 */ if(size>elementData.length-1) { Item[] newArray = (Item[])new Object[size*2]; //System.arraycopy(elementData, 0, newArray, 0, elementData.length);//也可以使用数组复制调用函数 for(int i=0;i<elementData.length;i++) { newArray[i] = elementData[i]; } elementData = newArray; //将elementData指向新的数组 } elementData[size++] = obj; } //根据索引删除某个对象 public Item remove(int index) { if(index<0||index>=size) //如果删除索引的位置小于0或大于数组长度,抛出异常 { try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } } Item oldValue = elementData[size-1]; //先保存值 for(int i=index;i+1<size;i++) { elementData[i] = elementData[i+1]; } elementData[--size] = null; //防止对象游离 return oldValue; } //删除 public boolean remove(Item item) { int j = 0; for(int i=0;i<size;i++) { if(item.equals(elementData[i])) { remove(i); return true; } } return false; } //根据索引获取某个元素 public Item get(int index) { if(index<0||index>=size) //判断不合法,抛出异常 { try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } } return elementData[index]; } @Override public Iterator<Item> iterator() { return new ArrayListIterator(); //返回一个迭代器对象 } private class ArrayListIterator implements Iterator<Item> { private int current = 0; @Override public boolean hasNext() { return current<size; } @Override public Item next() { // TODO Auto-generated method stub Item item1 = elementData[current++]; return item1; } public void remove() { } } } class MyArrayListTest { public static void main(String[] args) { myArrayList<String> arrayList = new myArrayList<>(3); //1.add方法的验证 arrayList.add("3"); arrayList.add("4"); arrayList.add("5"); arrayList.add("6"); //2.遍历方式输出所有的对象 for(int i=0;i<arrayList.size();i++) { System.out.println(arrayList.get(i)+" "); } //3.size()方法的验证 System.out.println("获取链表大小:"); System.out.println(arrayList.size()); //4.get(index)方法的验证 System.out.println("获取下标为2个元素的大小:"); System.out.println(arrayList.get(2)); //5.remove(int index)方法的验证 System.out.println("删除下标为2的元素:"); arrayList.remove(2); //6.删除"4"元素—根据集合元素值删除 arrayList.remove("4"); //使用迭代方式输出所有的对象 Iterator<String> it = arrayList.iterator(); while(it.hasNext()) { System.out.print(it.next()+" "); } } }
3.几点注意事项:
1)ArrayList的底层实现是数组,所以ArrayList适合查找,并且有顺序添加顺序取出的性质
2)为了尽可能加入各种类型的数据,ArrayList中的数组想使用泛型数组,但因为Java中没有泛型数组,所以定义泛型数组的时候要强制转化,例如Item[] item = (Item[])new Object[3];
3)数组的扩容,步骤第一是创建一个大小为原先大小两倍的数组,第二将原先数组中内容复制到数组中,第三注意将新数组指向旧数组。
4)ArrayList类的构造函数有默认构造函数和有带形参的构造函数,注意题中在默认构造函数中通过this指针调用代形参的构造函数。
4)在删除数组中的某个对象后,注意两点,第一是将后面的对象逐个向前移动,将最后的位置要设置为空,让垃圾回收器回收 ,防止对象游离。