实现数组_2_泛型
2019-04-11 本文已影响0人
苏州城外无故人
在创造自己的数组_1中,数组内存储数据只支持int类型,我们现在改成泛型。
package com.company;
/**
* @program: Array
* @description: 数组
* @author: Gu
* @create: 2019-04-10 22:03
**/
public class Array<E> {
private E[] data;
private int size;
//初始化
public Array(int capacity) {
data = (E[])new Object[capacity];
size = 0;
}
//无参构造函数默认最大容量为10
public Array() {
this(10);
}
//数组长度
public int length() {
return data.length;
}
//数组元素个数
public int getSize() {
return size;
}
//数组是否为空
public boolean isEmpty() {
return size == 0;
}
//向数组指定位置中添加元素
public void add(int index, E element) {
if (index >= data.length || index < 0) {
throw new IllegalArgumentException("[数组索引非法]");
}
if (size >= data.length) {
throw new IndexOutOfBoundsException("[数组已达到最大容量]");
}
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = element;
size++;
}
//向数组第一个索引添加元素
public void addFirst(E element) {
add(0, element);
}
//数组末尾添加元素
public void addLast(E element) {
add(size, element);
}
//根据index查找指定元素
public E get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("[数组索引非法]");
}
return data[index];
}
//查找数组是否包含指定元素
public boolean contains(E element) {
for (E value : data) {
if (value.equals(element)) {
return true;
}
}
return false;
}
//返回指定元素的索引
public int find(E element) {
for (int i = 0; i < size; i++) {
if (data[i].equals(element)) {
return i;
}
}
return -1;
}
//修改数组元素
public void set(int index, E element) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("[数组索引非法]");
}
data[index] = element;
}
//删除指定元素
public void delete(E element) {
int index = find(element);
if (index == -1) {
throw new IllegalArgumentException("[数组没有该元素]");
}
remove(index);
}
//根据索引删除元素
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("[数组索引非法]");
}
E res = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}
size = size - 1;
//改成泛型之后,最后一个内容要删除,清空内存,不删除也可以
data[size] = null;
return res;
}
//删除第一个
public E removeFirst() {
return remove(0);
}
//删除最后一个
public E removeLast() {
return remove(size - 1);
}
//打印数组相关信息
@Override
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(String.format("数组的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("]");
break;
}
stringBuffer.append(",");
}
return stringBuffer.toString();
}
}
测试类
package com.company;
public class Main {
public static void main(String[] args) {
//add()
Array<Integer> array = new Array<>(20);
for (int i = 0; i < 10; i++) {
array.add(i, i + 1);
}
System.out.println(array);
//[1,2,3,4,5,6,7,8,9,10]
//addFirst()
array.addFirst(0);
System.out.println(array);
//[0,1,2,3,4,5,6,7,8,9,10]
//addLast()
array.addLast(11);
System.out.println(array);
//[0,1,2,3,4,5,6,7,8,9,10,11]
// get()
System.out.println(array.get(0));
//0
System.out.println(array.contains(0));
//true
System.out.println(array.find(0));
//0
//set()
array.set(0, -2);
System.out.println(array);
//removeFirst
array.removeFirst();
System.out.println(array);
}
}
测试Student类
package com.company;
/**
* @program: Array
* @description: 用于数组泛型测试
* @author: Gu
* @create: 2019-04-11 20:31
**/
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
private void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return String.format("name : %s, age : %d", name, age);
}
public static void main(String[] args) {
Array<Student> array = new Array<>();
array.add(0,new Student("aa", 11));
array.add(1, new Student("bb",22));
System.out.println(array);
}
}