我已经搜索了互联网,但我找不到我的问题的答案。我正在编写自己的数组列表结构。我希望使它成为一个排序数组,它将接收来自联系人类的对象以按姓氏排序。到目前为止我有这个,但我无法管理如何执行 add() 方法。
public class SortedArrayList <E extends Comparable<E>> implements SortedList<E> {
private int currentSize;
private E elements[];
@SuppressWarnings("unchecked")
public SortedArrayList(int initialCapacity){
if(initialCapacity < 1){
throw new IllegalArgumentException("Need at least one element");
}
this.currentSize = 0;
this.elements = (E[]) new Object[initialCapacity];
}
@Override
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return new ListIterator<E>();
}
@Override
public boolean add(E obj) {
if(obj == null){
throw new IllegalArgumentException("object cannot be null.");
}
if(this.currentSize == this.elements.length){
reAllocate();
}
E temp[];
temp = elements;
for(int i=0; i<= this.currentSize; i++){
//if(obj.compareTo(elements[i]) < 0){
//int target =i;
//elements[i+1] = temp[i];
//elements[target] = obj;
//break;
}
}
this.elements[this.currentSize++] = obj;
return true;
}