JAVA数据结构---动态扩展内存自定义ArrayList
来源:互联网 发布:速读训练软件app 编辑:程序博客网 时间:2024/05/17 23:22
代码分为2个部分:
向量类:MyArrayList.java
测试类:MainTest.java
向量类:MyArrayList.java
@SuppressWarnings("unchecked")public class MyArrayList<T> { private T[] data;// 泛型数据 private int count;// 加入的元素个数 private final int DEFLAUT_SIZE = 10;// 默认的数组长度 private int listSize;// 实例化数组的长度 /** * 构造函数:有参数,无参数 */ public MyArrayList(int oldSize) { data = (T[]) (new Object[oldSize]); count = 0; listSize = oldSize; } public MyArrayList() { data = (T[]) (new Object[DEFLAUT_SIZE]); count = 0; listSize = DEFLAUT_SIZE; } /** * 私有的方法 */ private boolean isEnlarge() { boolean flag; if (isFull() == true) { T[] oldData = data; data = (T[]) (new Object[listSize + 1]); System.arraycopy(oldData, 0, data, 0, count); listSize++; flag = true; } else flag = false; return flag; } // 判断数组是否已经存满,这个是私有的方法。 // 既然是动态的数组,对外肯定是不会满的,所以设置为私有的。 private boolean isFull() { if (count == data.length) return true; else return false; } // 判断动态数组是否为空 private boolean isEmpty() { if (count == 0) return true; else return false; } /** * 公开的方法 */ // 在指定位置添加 public MyArrayList<T> add(int index, T t) { if (index > listSize) { throw new ArrayIndexOutOfBoundsException(index + ">" + listSize); } isEnlarge();// 检查是否需要扩充内存 // 在中间位置插入时候需要将后面的元素后移 if (index + 1 <= count) { for (int i = count - 1; i >= index; i--) data[i + 1] = data[i]; } // 插入元素 data[index] = t; count++; return this; } // 在尾部添加 public MyArrayList<T> add(T t) { add(count, t); return this; } // 删除元素 public MyArrayList<T> del(int index) throws Exception { if (isEmpty() == true) throw new Exception("数组为空无法删除..."); else if (isEmpty() == false) { if (index <= count - 2) { for (int i = index; i <= count - 2; i++) { data[i] = data[i + 1]; } count--; } else if (index == count - 1) { count--; } } return this; } // 删除最后一个元素 public MyArrayList<T> del() throws Exception { return del(count - 1); } // 修改 public MyArrayList<T> set(int index, T t) { data[index] = t; return this; } // 查询 public T get(int index) { if (index > count - 1 || index < 0) { System.out.println("下标错误..."); return null; } else return data[index]; } // 打印数组 public void printList() { for (int i = 0; i < count; i++) { System.out.print(data[i] + " "); } System.out.println(); System.out.println("长度是" + count); }}
测试类:MainTest.java
public class MainTest { public static void main(String[] args) throws Exception { MyArrayList<String> mal = new MyArrayList<String>(3); mal.add("000").printList(); mal.add(0, "111").printList(); mal.add(1, "222").printList(); mal.add("222").printList(); mal.add("333").printList(); mal.add("444").printList(); mal.add("666").printList(); mal.add(2, "YYY").printList(); mal.del().printList(); mal.del(2).printList(); mal.del(0).printList(); mal.add("999").printList(); System.out.println(mal.get(1)); mal.set(0, "9090").printList(); }}
测试结果:
000 长度是1111 000 长度是2111 222 000 长度是3111 222 000 222 长度是4111 222 000 222 333 长度是5111 222 000 222 333 444 长度是6111 222 000 222 333 444 666 长度是7111 222 YYY 000 222 333 444 666 长度是8111 222 YYY 000 222 333 444 长度是7111 222 000 222 333 444 长度是6222 000 222 333 444 长度是5222 000 222 333 444 999 长度是60009090 000 222 333 444 999 长度是6
0 0
- JAVA数据结构---动态扩展内存自定义ArrayList
- ArrayList的动态扩展
- ArrayList的动态扩展过程
- java中的数据结构--ArrayList
- Java数据结构-ArrayList
- java数据结构之ArrayList
- java之自定义ArrayList
- Java动态数组ArrayList
- ArrayList,java动态数组
- 【数据结构】动态内存管理
- 数据结构--动态内存管理
- 数据结构--ArrayList的Java实现
- Java数据结构 ArrayList、LinkList、Vector
- 数据结构-Java实现-ArrayList&LinkedList
- JAVA数据结构-LinkedList,ArrayList,Tree,
- Java数据结构源码分析-ArrayList
- java数据结构 ArrayList源码阅读
- Java数据结构之ArrayList(4)
- 科普:alphago是什么
- 设计模式(三)观察者模式Observer(发布订阅)
- 解决安卓加载长图出现解码失败导致显示空白的问题
- HDU-2084(数塔)(动态规划)
- JavaMail--群发HTML邮件
- JAVA数据结构---动态扩展内存自定义ArrayList
- iOS蓝牙4.0开发
- 【html5和css3】transition属性整理
- Android 混淆代码
- 使用poi 导出 excel
- UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout
- 理解Java垃圾回收机制
- js获取下拉框值
- Android Properties 使用实例