package DataStructure.Arry;
public class Array<T> {
private T[] data;
private int size;
public Array(int capacity){
data =(T[]) new Object[capacity];
size = 0;
}
public Array(){
this(10);
}
public int agetSize(){
return size;
}
public int getCapacity(){
return data.length;
}
public boolean isEmpty(){
return size == 0;
}
public void addLast(T e){
if(size == data.length){
throw new RuntimeException("长度不够");
}
data[size] =e;
size++;
}
public void addFirst(T e){
add(0,e);
}
public void add(int index,T e){
if(size == data.length){
throw new RuntimeException("长度不够");
}
if(index <0 || index >= size){
throw new RuntimeException("非法的参数");
}
for(int i = size-1 ; i>=index ;i--){
data[i+1] = data[i];
}
data[index] = e;
size++;
}
public T get(int index){
if(index <0 || index>=size){
throw new RuntimeException("长度不够");
}
return data[size];
}
public void set(int index,T e){
if(index <0 || index>=size){
throw new RuntimeException("长度不够");
}
data[index] = e;
}
public T remove(int index){
if(index <0 || index>=size){
throw new RuntimeException("长度不够");
}
T ret =data[index];
for(int i =index+1;i<size;i++){
data[i-1]=data[i];
}
size--;
data[size] = null;
return ret;
}
public void removeElement(T e){
for(int i = 0 ; i<size;i++){
if(data[i].equals(e)){
for(int z = i+1;z<size;z++){
data[z-1] = data[z];
}
}
}
size--;
}
@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();
}
}