java如何按值而不是位置将项目添加到ADTSorted列表
因此,我创建了一个SortedDoubleList,它是已排序的ADT列表。要将对象插入数组,我有:
Public void ListInsert(int posi, int newItem){
throws RuntimeException, IndexOutOfBoundsException{
if (numItems>=LISTSIZE) {
//statment
throw new RuntimeException("List is full and you cannot add new item");
}else{
//shift element
if (posi>=1 and posi<=numItems+1) {
//backward
for (int i=numItems; i>=posi ; i-- ) {
items[i]=items[i-1];
}
items[posi-1]=newItem;
numItems++;
}else{
throws new IndexOutOfBoundsException("index of new item to be inserted out of bound")
}
}
}
}
但不是通过职位来增加,而是通过价值来增加。例如,在排序列表中插入项目时,不指定该项目在列表中所属的位置。相反,插入操作通过将项目的值与列表中现有项目的值进行比较来确定项目的正确位置。这个数字也是双倍的!你对此有什么想法吗? 谢谢
# 1 楼答案
你说的“按值插入”是什么意思?无法指定位置,因为列表将被取消排序。假设你试着在第二个位置插入3到这个列表{1,2,4},这个列表就是{1,3,2,4}