0 课程地址
https://coding.imooc.com/lesson/207.html#mid=13412
1 重点关注
1.1 数组动态伸缩
参见3.1 coding
1.2 泛型数组
参见3.2 coding
1.3 无参调有参的简洁方式
参见3.2 coding
2 课程内容
见3
3 Coding
3.1 动态数据伸缩
- 精简:
//3.3 数组根据索引添加元素 public void addElement(int index,E e){ //这里扩展数组长度写到行首,不在这扩下边异常 if(size== data.length){ //todo 并不会,需要把值一条一条的赋进来 resize(2*size); } //... } //5.3 数组删除,通常情况下做删除,会在出参把删除的值带出来 public E remove(int index){ //... // 这块逻辑写到末尾是自己没有想到的,多动脑啊 if(size == data.length/2){ resize(data.length/2); } return outParm; } //6.1 数组动态伸缩 这里用size更好,想想为什么 private void resize(int capacity){ E[] newData = (E[]) new Object[capacity]; for(int i = 0;i < size;i++){ newData[i] = data[i]; } data = newData; }
- 完整:
package com.company; import java.util.Arrays; public class ArrayFan<E> { private int size; //int类型的数组 private E[] data; //1.1 创建构造函数,传入容量,则新生成一个数组 public ArrayFan(int capacity){ data = (E[]) new Object[capacity]; size = 0; } //1.2 创建无参构造函数 public ArrayFan(){ this(10); } //1.3 添加传入静态数组的构造函数 public ArrayFan(E[] param){ this.data = param; long outParm = Arrays.stream(param).filter(e->{ return e!=null; }).count(); this.size = (int)outParm; } //2.1 添加getSize,获取数组元素个数 public int getSize(){ return size; } //2.2 添加getCapacity,获取数组容量 public int getCapacity(){ return data.length; } //2.3 添加数组是否为空方法 public boolean isEmpty(){ return size==0; } //3.1 在数组末尾添加元素 public void addLast(E e){ addElement(size,e); } //3.2 在数组起始添加元素 public void addFirst(E e){ addElement(0,e); } //3.3 数组根据索引添加元素 public void addElement(int index,E e){ //1 校验异常 //1.1 如果数组已经满了,则禁止插入 if(size== data.length){ //todo 并不会,需要把值一条一条的赋进来 resize(2*size); //throw new IllegalArgumentException("数组已满,禁止插入"); } //1.2 如果传入的索引在已有数组的索引之外,则校验异常 if(index<0||index>size){ throw new IllegalArgumentException("索引应在已有数组的索引之间"); } //2 实现根据索引添加元素的逻辑 //2.1 data同步 for(int j = size-1;j>=index;j--){ data[j+1] = data[j]; } data[index] = e; //2.2 size同步 size++; } //6.1 数组动态伸缩 这里用size更好,想想为什么 private void resize(int capacity){ E[] newData = (E[]) new Object[capacity]; for(int i = 0;i < size;i++){ newData[i] = data[i]; } data = newData; } //4.1 数组 toString 范例 @Override public String toString() { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(String.format("Array:size = %d,capacity = %d\n",size,data.length)); stringBuffer.append("["); for(int i=0;i<size;i++){ stringBuffer.append(data[i]); if(i!=size-1){ stringBuffer.append(","); } } stringBuffer.append("]"); return stringBuffer.toString(); } //4.2 get获取元素 public E get(int index){ if(index<0||index>data.length){ throw new IllegalArgumentException("111"); } return data[index]; } //4.3 set获取元素 public void set(int index,E e){ if(index<0||index>data.length){ throw new IllegalArgumentException("111"); } data[index] = e; } //5.1 数组包含 public boolean contails(E e){ for(int i = 0;i<size;i++){ if(e.equals(data[i])){ return true; } } return false; } //5.2 数组搜索 public int search(E e){ for(int i = 0;i<size;i++){ if(e.equals(data[i])){ return i; } } return -1; } //5.3 数组删除,通常情况下做删除,会在出参把删除的值带出来 public E remove(int index){ if(index<0||index>=size){ throw new IllegalArgumentException("111"); } E outParm = data[index]; for(int i=index;i<size-1;i++){ data[i] = data[i+1]; } //这块不塞值也没有任何影响,因为size已经--了,不会访问到size之外的元素 data[size-1]= null; size--; if(size == data.length/2){ resize(data.length/2); } return outParm; } //5.4 删除首个元素 public E removFirst(){ return remove(0); } //5.5 删除最后的元素 public E removLast(){ return remove(size-1); } //5.6 删除指定的元素 public void removElement(E e){ int index = -1; //判断删除的元素是否存在 for(int i=0;i<size;i++){ if(e.equals(data[i])){ index = i; break; } } if(index>=0){ remove(index); }else{ throw new IllegalArgumentException("删除的元素未找到"); } } }
- 测试
public static void main(String[] args) { ArrayFan<Integer> arrayFan = new ArrayFan<>(); for(int i = 0; i< 10;i++){ arrayFan.addLast(i); } arrayFan.addLast(100); System.out.println(arrayFan); arrayFan.removLast(); System.out.println(arrayFan); arrayFan.removLast(); System.out.println(arrayFan); }
- 测试结果:
---- IntelliJ IDEA coverage runner ---- sampling ... include patterns: exclude patterns: Array:size = 11,capacity = 20 [0,1,2,3,4,5,6,7,8,9,100] Array:size = 10,capacity = 10 [0,1,2,3,4,5,6,7,8,9] Array:size = 9,capacity = 10 [0,1,2,3,4,5,6,7,8] Class transformation time: 0.0472355s for 141 classes or 3.350035460992908E-4s per class Process finished with exit code 0
3.2 泛型数组
- 精简(完整见3.1)
public class ArrayFan<E> { private int size; //int类型的数组 private E[] data; //1.1 创建构造函数,传入容量,则新生成一个数组 public ArrayFan(int capacity){ data = (E[]) new Object[capacity]; size = 0; } //1.2 创建无参构造函数 public ArrayFan(){ this(10); } //1.3 添加传入静态数组的构造函数 public ArrayFan(E[] param){ this.data = param; long outParm = Arrays.stream(param).filter(e->{ return e!=null; }).count(); this.size = (int)outParm; } }
标签:capacity,int,玩转,数组,数据结构,data,public,size From: https://www.cnblogs.com/1446358788-qq/p/16783275.html