代码拉取完成,页面将自动刷新
package second_term.sixth_chapter;
import second_term.first_week.EmptyCollectionException;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public abstract class ArrayList<T> implements ListADT<T>,Iterable<T> {
private final static int DEFAULT_CAPACITY = 100;
private final static int NOT_FOUND = -1;
protected int rear;
protected T[] list;
protected int modCount;
public ArrayList(){
this(DEFAULT_CAPACITY);
}
public ArrayList(int initialCapacity){
rear =0 ;
list =(T[])(new Object[initialCapacity]);
modCount = 0;
}
public T remove(T element){
T result;
int index = find(element);
if (index == NOT_FOUND)
throw new ElementNotFoundException("ArrayList");
result = list[index];
rear--;
for (int scan=index;scan<rear;scan++){
list[scan]=list[scan+1];
}
list[rear] = null;
modCount++;
return result;
}
protected int find(T element){
int scan = 0;
int result = NOT_FOUND;
if (!isEmpty()){
while (result == NOT_FOUND&&scan<rear){
if (element.equals(list[scan]))
result =scan;
else
scan++;
}
}
return result;
}
public boolean contains(T target){
return (find(target) != NOT_FOUND);
}
protected void expandCapacity()
{
// T[] temp =(T[])(new Object[list.length+1]);
// for (int i =0;i<list.length;i++){
// temp[i] = list[i];
// }
// list = temp;
list = Arrays.copyOf(list, list.length);
}
@Override
public boolean isEmpty() {
return rear ==0;
}
@Override
public T removefirst() throws EmptyCollectionException{
if (isEmpty())
throw new EmptyCollectionException("ArrayList");
else {
T result = list[0];
for (int scan = 0; scan < rear; scan++) {
list[scan] = list[scan + 1];
}
list[rear] = null;
return result;
}
}
@Override
public T removeLast() throws EmptyCollectionException{
if (isEmpty())
throw new EmptyCollectionException("ArrayList");
else{
T result =list[rear];
list[rear]=null;
return result;
}
}
@Override
public T first() throws EmptyCollectionException{
if (isEmpty())
throw new EmptyCollectionException("ArrayList");
else
return list[0];
}
@Override
public T last() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("ArrayList");
else
return list[rear];
}
@Override
public int size() {
return rear;
}
public String toString(){
String result = "";
for (int i=0;i<rear;i++){
result+= list[i]+" ";
}
return result;
}
public Iterator<T> iterator()
{
return new ArrayListIterator();
}
/**
* ArrayListIterator iterator over the elements of an ArrayList.
*/
private class ArrayListIterator implements Iterator<T>
{
int iteratorModCount;
int current;
public ArrayListIterator()
{
iteratorModCount = modCount;
current = 0;
}
public boolean hasNext() throws ConcurrentModificationException
{
if (iteratorModCount != modCount)
throw new ConcurrentModificationException();
return (current < rear);
}
public T next() throws ConcurrentModificationException
{
if (!hasNext())
throw new NoSuchElementException();
current++;
return list[current - 1];
}
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。