0 课程地址
https://coding.imooc.com/lesson/207.html#mid=13411
1 重点关注
1.1 泛型改造
==转equals
详见3
2 课程内容
见3
3 Coding
3.1 相关coding
- code
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; } //todo //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){ 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++; } //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--; 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> array = new ArrayFan<>(20); array.addFirst(1); array.addElement(1,2); array.addElement(0,3); array.addLast(7); System.out.println(array.contails(7)); System.out.println(array.contails(8)); System.out.println(array.search(4)); System.out.println(array.search(8)); System.out.println(array.remove(1)); System.out.println(array); System.out.println(array.removFirst()); System.out.println(array); System.out.println(array.removLast()); System.out.println(array); }