2 Star 0 Fork 0

CS-IMIS-23/zc20172324

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
OrderedArray.java 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
zc20172324 提交于 7年前 . pp6.8
package chap6;
import chap3.EmptyCollectionException;
public class OrderedArray<T> extends ArrayList<T>
implements OrderedListADT<T>
{
/**
* Creates an empty list using the default capacity.
*/
public OrderedArray()
{
super();
}
/**
* Creates an empty list using the specified capacity.
*
* @param initialCapacity the initial size of the list
*/
public OrderedArray(int initialCapacity)
{
super(initialCapacity);
}
/**
* Adds the specified Comparable element to this list, keeping
* the elements in sorted order.
*
* @param element the element to be added to the list
*/
public void add(T element)
{
if (!(element instanceof Comparable))
throw new NonComparableElementException("OrderedList");
Comparable<T> comparableElement = (Comparable<T>)element;
if (size() == list.length)
expandCapacity();
int scan = 0;
// find the insertion location
while (scan < rear && comparableElement.compareTo(list[scan]) > 0)
scan++;
// shift existing elements up one
for (int shift=rear; shift > scan; shift--)
list[shift] = list[shift-1];
// insert element
list[scan] = element;
rear++;
modCount++;
}
@Override
public T removeFirst() throws EmptyCollectionException
{
if (isEmpty()){
throw new EmptyCollectionException("ArrayList");
}
T result ;
result= list[0];
for (int i = 0; i < rear - 1;i++){
list[i] = list[i + 1];
}
list[rear - 1] = null;
rear--;
modCount++;
return result;
}
@Override
public T removeLast() throws EmptyCollectionException
{
if (isEmpty()){
throw new EmptyCollectionException("ArrayList");
}
T result ;
result= list[list.length-1];
list[rear] = null;
rear--;
modCount++;
return result;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/zc20172324.git
git@gitee.com:CS-IMIS-23/zc20172324.git
CS-IMIS-23
zc20172324
zc20172324
master

搜索帮助