package com.atguigu.structure; public class Demo02_arrayList { public static void main(String[] args) { MyGenericArrayListV1 arrayListV1 = new MyGenericArrayListV1(); //arr.add(element:100,index:1);下标越界,无法插入 arrayListV1.add(10, 0); arrayListV1.add(20, 1); arrayListV1.add(30, 2); arrayListV1.print();//10 20 30 0 0 arrayListV1.add(40,0); arrayListV1.add(50,0); arrayListV1.print();//50 40 10 20 30 arrayListV1.printRev();//30 20 10 40 50 //移除index=0数据前 elements=[50,40,10,20,30] size=5 arrayListV1.remove(0); //移除index=0数据后 elements=[40,10,20,30,30] size=4 //头插前 elements=[40,10,20,30,30] size=4 arrayListV1.addFirst(50); //头插后 elements=[50,40,10,20,30] size=5 //移除index=4数据前 elements=[50.40,10,20,30] size=5 arrayListV1.remove(4); //移除index=4数据后 elements=[50,40,10,20,30] size=4 //尾插前 size=4 elements=[50,40,10,20,30] arrayListV1.addLast(30); //尾插后 size=5 elements=[50,40,10,20,30] //修改index=0数据前 elements=[50,40,10,20,30] size=5 arrayListV1.update(100, 0); //修改index=0数据后 elements=[100,40,10,20,30] size=5 //查询index=0数据 elements=[100,40,10,20,30] size=5 arrayListV1.get(0); } } class MyGenericArrayListV1{ //组织存储数据的结构 private int[] elements; //数据关系的属性 //The size of the ArrayList (the number of elements it contains) //ArrayList集合的大小(包含元素的数量) private int size; public MyGenericArrayListV1() { this(5); } public MyGenericArrayListV1(int capacity) { elements = new int[capacity]; size = 0; } //操作数据的方法: //1、新增:根据下标插入数据、头插 、 尾插 public void addFirst(int element) { //element=50 elements=[40,10,20,30,30] size=4 this.add(element, 0); //element=50 elements=[50,40,10,20,30] size=5 } public void addLast(int element) { //尾插前 element=30 size=4 elements=[50,40,10,20,30] this.add(element, size); //尾插后 element=30 size=5 elements=[50,40,10,20,30] } public void add(int element, int index) {//参数1:要新增的数据 参数2:要插入的位置下标 //头插前 element=50 index=0 size=4 elements=[40,10,20,30,30] //尾插前 element=30 index=4 size=4 elements=[50,40,10,20,30] if (isFull()) { System.out.println("数组已满,无法插入"); return; } //数组数据是连续存储的,不能跳过未存储数据的位置 //头插前 index=0 size=4 element=50 elements=[40,10,20,30,30] //尾插前 index=4 size=4 element=30 elements=[50,40,10,20,30] if (index<0||index>size) { System.out.println("下标越界,无法插入"); return; } //如果新增时,数组中没有元素,添加到第一个索引位置 直接存入 //如果位置上已存在元素 当前索引位置旧元素需要往后移动 //[1,2,3,4,0] => 将 5 插入到索引为 index(1) 的位置 [1,5,2,3,4] ==>设置5到索引1位置 //[1,2,3,0,0] => 将 5 插入到索引为 index(1) 的位置 [1,5,2,3,0] ==>设置5到索引1位置 //判断要存入的位置是否需要移动元素 //index=0 size=4 3>0 满足条件,进入循环 element=50 elements=[40,10,20,30,30] //index=0 size=4 i=3 elements=[40,10,20,30,30] //index=0 size=4 i=2 elements=[40,10,20,20,30] //index=0 size=4 i=1 elements=[40,10,10,20,30] //index=0 size=4 i=0 elements=[40,40,10,20,30] //尾插前 index=4 size=4 3>4 不满足条件,循环进不去 elements=[50,40,10,20,30] for (int i = size - 1; i >= index; i--) { //i=3 elements=[40,10,20,30,30] elements[3]=30 elements[4]=30 //i=2 elements=[40,10,20,30,30] elements[2]=20 elements[3]=30 //i=1 elements=[40,10,20,20,30] elements[1]=10 elements[2]=20 //i=0 elements=[40,10,10,20,30] elements[0]=40 elements[1]=10 elements[i + 1] = elements[i]; } //头插前 element=50 index=0 elements=[40,40,10,20,30] size=4 elements[0]=40 //尾插前 element=30 index=4 elements=[50,40,10,20,30] size=4 elements[4]=30 elements[index] = element; //头插后 size=4 elements=[50,40,10,20,30] element=50 index=0 //尾插后 size=4 elements=[50,40,10,20,30] element=30 index=4 size++; //头插 size=5 element=50 index=0 elements=[50,40,10,20,30] //尾插 size=5 element=30 index=4 elements=[50,40,10,20,30] } //2、删除 public Integer remove(int index) { //移除index=0数据前 size=5 isEmpty返回false进不去if //移除index=4数据前 size=5 elements=[50,40,10,20,30] if (isEmpty()) { System.out.println("数组为空,删除失败"); return null; } //移除index=0数据前 size=5 elements[0]=50 elements=[50,40,10,20,30] //移除index=4数据前 size=5 elements[4]=30 elements=[50,40,10,20,30] if (index < 0 || index >= size) { System.out.println("下标越界。删除失败"); return null; } //移除index=0数据前 index=0 elements=[50,40,10,20,30] elements[0]=50 //移除index=4数据前 index=4 elements=[50,40,10,20,30] elements[4]=30 int remE = elements[index]; //将它后面的元素前移 //移除index=0数据前 index=0 size=5 elements=[50,40,10,20,30] elements[0]=50 //移除index=0数据后 index=0 size=5 elements=[40,40,10,20,30] elements[0]=40 // index=0 i=1 size=5 elements=[40,10,10,20,30] elements[1]=10 // index=0 i=2 size=5 elements=[40,10,20,20,30] elements[2]=20 // index=0 i=3 size=5 elements=[40,10,20,30,30] elements[3]=30 //移除index=4数据前 index=4 size=5 elements=[50,40,10,20,30] remE=30 进不去循环 for (int i = index; i < size - 1; i++) { //移除index=0数据前 i=0 elements=[50,40,10,20,30] elements[0]=50 elements[1]=40 //移除index=0数据后 i=1 elements=[40,40,10,20,30] elements[1]=40 elements[2]=10 // i=2 elements=[40,10,10,20,30] elements[2]=50 elements[3]=20 // i=3 elements=[40,10,20,20,30] elements[3]=20 elements[4]=30 elements[i] = elements[i + 1]; } //size记录了元素个数:遍历获取数据使用size作为边界 //移除index=0数据 size=5 remE=50 elements=[40,10,20,30,30] //移除index=4数据 size=5 remE=30 elements=[50,40,10,20,30] size--; //移除index=0数据 size=4 remE=50 elements=[40,10,20,30,30] //移除index=4数据 size=4 remE=30 elements=[50,40,10,20,30] return remE; } //3、修改 public Integer update(int element, int index) { //修改index=0数据前 element=100 index=0 size=5 if (isEmpty()) { System.out.println("数组为空,无法修改"); return null; } //修改index=0数据前 size=5 elements=[50,40,10,20,30] element[0]=50 if (index < 0 || index >= size) { System.out.println("下标越界,无法修改"); return null; } //修改index=0数据前 element=100 elements=[50,40,10,20,30] elements[0]=50 int oldE = this.elements[index]; //修改index=0数据前 element=100 elements=[50,40,10,20,30] oldE=50 elements[0]=50 this.elements[index] = element; //修改index=0数据前 oldE=50 elements=[100,40,10,20,30] elements[0]=100 return oldE; } //4、查询 public Integer get(int index) { //查询index=0数据前 size=5 elements=[100,40,10,20,30] if (isEmpty()) { System.out.println("数组为空"); return null; } //查询index=0数据前 size=5 elements[0]=100 elements=[100,40,10,20,30] if (index < 0 || index >= size) { System.out.println("下标越界,查询失败"); return null; } //查询index=0数据后 elements=[100,40,10,20,30] elements[0]=100 size=5 return this.elements[index]; } //辅助方法:新增数据时size++,移除数据时size-- //是否为空 public boolean isEmpty() { //移除index=0数据前 size=5 不等于0 方法返回false //移除index=4数据前 size=5 不等于0 方法返回false //修改index=0数据前 size=5 不等于0 方法返回false //查询index=0数据前 size=5 不等于0 方法返回false return size == 0; } //是否已满 public boolean isFull() { //头插前 elements=[40,10,20,30,30] size=4 elements.length=5 //尾插前 elements=[50,40,10,20,30] size=4 elements.length=5 return size == this.elements.length; } //正序打印 public void print() { if (isEmpty()) { return; } for (int i = 0; i < this.size; i++) { System.out.print(this.elements[i] + "\t"); } System.out.println(); } //反序打印 public void printRev() { if (isEmpty()) { return; } int length = size; while (--length >= 0) {//第一次使用数组长度-1,当做下标取最后一个位置的元素打印 //第二次使用长度-2 System.out.print(this.elements[length]+"\t"); } System.out.println(); } }