2 Star 0 Fork 0

CS-IMIS-23/20172309_javaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ArrayList.java 4.12 KB
一键复制 编辑 原始数据 按行查看 历史
20172309 提交于 2018-10-06 09:42 . PP6.8 完成ArrayList类的实现
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();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172309_javaProgramming.git
git@gitee.com:CS-IMIS-23/20172309_javaProgramming.git
CS-IMIS-23
20172309_javaProgramming
20172309_javaProgramming
master

搜索帮助