我已經編寫了用於插入和從數組中移除元素的代碼。但我想按排序順序將元素插入到數組中。我該如何改進我的「添加」方法?我也不知道「刪除」方法的實施。我怎樣才能實現remove方法按排序順序將元素插入到數組中
public void add(int index, String str) {
// First make sure the index is valid.
if (index > elements || index < 0) {
throw new IndexOutOfBoundsException();
}
// If the list is full, resize it.
if (elements == list.length) {
resize();
}
// Shift the elements starting at index
// to the right one position.
for (int index2 = elements; index2 > index; index2--) {
list[index2] = list[index2 - 1];
}
// Add the new element at index.
list[index] = str;
// Adjust the number of elements.
elements++;
}
public boolean remove(String str) {
return false;
}
插入元素對數組進行排序後。最後你想要排序的數組。 – Visme 2014-11-06 12:29:36
看[這裏](http://stackoverflow.com/questions/8725387/why-there-is-no-sortedlist-in-java)mybe這有助於。 – Jens 2014-11-06 12:29:41