public class MyArray<T> {
private Object array[];
private int elements;
public MyArray() {
array = new Object[50];
}
public MyArray(int maxSize) {
array = new Object[maxSize];
}
/**
*
* @Title: add
* @Description: 添加数据
* @param value
*/
public void add(T value) {
array[elements] = value;
elements++;
}
/**
*
* @Title: display
* @Description: 显示数据
*/
public void display() {
System.out.print("[ ");
for (int i = 0; i < elements; i++) {
System.out.print(array[i] + " ");
}
System.out.println("]");
}
/**
*
* @Title: size
* @Description: 数据中的有效数据长度
* @return
*/
public int size() {
return elements;
}
/**
*
* @Title: search
* @Description: 查找
* @param index
* @return
*/
public Object search(int index) {
if (index < 0 || index >= elements) {
throw new ArrayIndexOutOfBoundsException();
} else {
return array[index];
}
}
/**
*
* @Title: delete
* @Description: 删除
* @param index
*/
public void delete(int index) {
if (index < 0 || index >= elements) {
throw new ArrayIndexOutOfBoundsException();
} else {
for(int i=index;i<elements - 1;i++) {
array[i] = array[i + 1];
}
elements --;
}
}
}
数组
最新推荐文章于 2022-10-18 19:32:42 发布