public class Array<TypeE> {
private TypeE[] data;
private int size;
//有参构造函数
public Array(int capacity)
{
data = (TypeE[]) new Object[capacity];
size = 0;
}
//无参构造函数
public Array(){
this(10);
System.out.println("Here");
}
// 获取index索引位置的元素
TypeE get(int index)
{
if(index < 0 || index >= size)
throw new IllegalArgumentException("Get failed, Index is illegal");
return data[index];
}
//修改index索引位置的元素为e
void set(int index, TypeE e){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Get failed, Index is illegal");
data[index] = e;
}
public int getSize(){
return size;
}
public int getCapacity(){
return data.length;
}
public boolean isEmpty(){
return size == 0;
}
public void addLast(TypeE e){
add(size, e);
}
public void addFirst(TypeE e)
{
add(0, e);
}
public void add(int index, TypeE 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 || index > size.");
}
for (int i = size - 1; i >= index; i--){
data[i + 1] = data [i];
}
data[index] = e;
size ++;
}
//包含
public boolean contains(TypeE e){
for (int i = 0; i < size; i++) {
if( data[i].equals(e))//引用比较
return true;
}
return false;
}
//查找数组中元素e所在的索引如果不存在元素e,则-1
public int find(TypeE e){
for (int i = 0; i < size; i++) {
if( data[i].equals(e))//引用比较
return i;
}
return -1;
}
//删除元素,返回删除的元素
public TypeE remove(int index){
if(index < 0 || index >= size)
throw new IllegalArgumentException("Get failed, Index is illegal");
TypeE retValue = data[index];
for (int i = index + 1; i < size; i++) {
data[i-1] = data[i];
}
size --;
data[size] = null;//不是必须的 loitering objects != memory leak
return retValue;
}
public TypeE removeFirst()
{
return remove(0);
}
public TypeE removeLast(){
return remove(size - 1);
}
public void removeElement(TypeE e)
{
int index = find(e);
if(index != -1)
remove(index);
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
res.append('[');
for (int i = 0; i < size; i++)
{
res.append(data[i]);
if( i != size -1)
res.append(", ");
}
res.append(']');
return res.toString();
}
}
public class Main {
public static void main(String[] args) {
Array<Integer> arr = new Array<>(20);
for (int i = 0; i < 10; i++) {
arr.addLast(i);
}
System.out.println(arr);
arr.add(1, 100);
System.out.println(arr);
arr.addFirst(-1);
System.out.println(arr.getSize());
System.out.println(arr.toString());
arr.remove(2);
System.out.println(arr);
arr.removeElement(4);
System.out.println(arr);
arr.removeFirst();
System.out.println(arr);
arr.removeLast();
System.out.println(arr);
}
}
public class Student {
private String name;
private int score;
public Student(String stuName, int stuScore){
name = stuName;
score = stuScore;
}
@Override
public String toString(){
return String.format("Student(name: %s, score: %d)", name, score);
}
public static void main(String[] args)
{
Array<Student> arr = new Array<>();
arr.addLast(new Student("alice", 100));
arr.addLast(new Student("Michael", 88));
System.out.println(arr);
}
}