泛型数组
//泛型数组
public class Array<E>{
private E[] data;
private int size;//记录元素个数
//构造函数,用户创建数组开辟的空间
public Array(int capacity) {
// new 一个E类型的数组
data =(E[]) new Object[capacity];
size = 0;
}
//用户不传参时,默认开辟空间10
public Array() {
this(10);
}
//获取数组中元素个数
public int getSize() {
return size;
}
//获取空间大小
public int getCapacity() {
return data.length;
}
//判断数组是否为空
public boolean isEmpty() {
return size == 0;
}
//在数组最后添加元素2-3
public void addLast(E e) {
// if (size == data.length)
// throw new IllegalArgumentException("Addlast failed.Array is full");
// data[size] = e;
// size++;
add(size,e);
}
//在首位置添加元素
public void addFirst(E e){
add(0,e);
}
//指定位置添加元素
//元素从后往前挪把指定位置空出来
public void add(int index,E e){
if (size == data.length)
throw new IllegalArgumentException("Addlast failed.Array is full");
if (index<0||index>size)
throw new IllegalArgumentException("Add failed.Require index>=0 and index<=size");
for (int i=size-1;i>=index;i--){
data[i+1]=data[i];
}
data[index]=e;
size++;
}
//获取index索引的位置
public E get(int index){
if (index<0||index>=size)
throw new IllegalArgumentException("Add failed.Require index>=0 and index<=size");
return data[index];
}
//更新数组元素
public void set(int index,E e){
if (index<0||index>=size)
throw new IllegalArgumentException("Add failed.Require index>=0 and index<=size");
data[index]=e;
}
//是否包含某元素
public boolean contains(E e){
for (int i = 0; i < size; i++) {
if (e.equals(data[i]))
return true;
}
return false;
}
//返回所要查找的元素的索引
public int find(E e){
for (int i = 0; i < size; i++) {
if (e.equals(data[i]))
return i;
}
return -1;
}
//删除索引处的某个元素并返回,将索引之后的元素向前移动
public E remove(int index){
if (index<0||index>size)
throw new IllegalArgumentException("Add failed.Require index>=0 and index<=size");
E result=data[index];
for (int i = index+1; i < size; i++) {
data[i-1]=data[i];
}
size--;
// data[size]=null;
return result;
}
//删除头元素
public E removeFirst(){
return remove(0);
}
//删除最后一个元素
public E removeLast(){
return remove(size-1);
}
//删除某个元素
public void removeElement(E e){
int index = find(e);
if (index!=-1)
remove(index);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("Array:size=%d,capacity =%d\n",size,data.length));
stringBuilder.append("[");
for (int i=0;i<size;i++){
stringBuilder.append(data[i]);
if (i!=size-1)
stringBuilder.append(",");
}
stringBuilder.append("]");
return stringBuilder.toString();
}
}
test
public class Main {
public static void main(String[] args) {
Array<Integer> array = new Array(20);
for (int i=0;i<10;i++){
array.addLast(i);
}
System.out.println(array);
array.add(1,100);
System.out.println(array);
array.addFirst(-1);
System.out.println(array);
array.remove(2);
System.out.println(array);
array.removeElement(4);
System.out.println(array);
array.removeFirst();
System.out.println(array);
}
}
测试
public class Student {
private String name;
private int scores;
public Student(String name, int scores) {
this.name = name;
this.scores = scores;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", scores=" + scores +
'}';
}
public static void main(String[] args) {
Array<Student> array = new Array<>();
array.addLast(new Student("张三",90));
array.addLast(new Student("李四",98));
array.addLast(new Student("王五",88));
System.out.println(array);
}
}