用到的类和接口

IarrayList接口
public interface IArrayList {
public void display();
public void add(int data);
public void add(int pos, int data);
public boolean contains(int toFind);
public int indexOf(int toFind);
public int get(int pos);
public boolean isEmpty();
public void set(int pos, int value);
public void remove(int key);
public int size();
public void clear();
}
PosNotLegalException 异常(用来判断pos传参合法性)
public class PosNotLegalException extends RuntimeException {
public PosNotLegalException(){
}
public PosNotLegalException(String msg){
super(msg);
}
}
MyArrayList类
import java.util.Arrays;
public class MyArrayList implements IArrayList {
public int[] elem;
public int usedSize;
private static final int DEFAULT_SIZE = 10;
public MyArrayList() {
this.elem = new int[DEFAULT_SIZE];
}
private int[] dilatation(int[] oldArray, int newSize) {
return Arrays.copyOf(oldArray, newSize);
}
@Override
public void display() {
for (int i = 0; i < usedSize; i++) {
System.out.print(elem[i] + " ");
}
System.out.println();
}
@Override
public void add(int data) {
if (isFull()) {
elem = dilatation(elem, 2 * elem.length);
}
elem[usedSize] = data;
usedSize++;
}
private boolean isFull() {
return usedSize == elem.length;
}
private void checkPosInAdd(int pos) throws PosNotLegalException {
if (pos < 0 || pos > usedSize) {
throw new PosNotLegalException("pos位置不合法...");
}
}
private void checkPosInSetAndGet(int pos)throws PosNotLegalException{
if(pos<0 || pos>=usedSize){
throw new PosNotLegalException("pos位置不合法...");
}
}
@Override
public void add(int pos, int data) {
try {
checkPosInAdd(pos);
} catch (PosNotLegalException e) {
e.printStackTrace();
}
elem = isFull() ? dilatation(elem, 2 * elem.length) : elem;
for (int i = usedSize; i >= pos; i--) {
elem[i + 1] = elem[i];
}
elem[pos] = data;
usedSize++;
}
@Override
public boolean contains(int toFind) {
for (int i = 0; i < usedSize; i++) {
if (toFind == elem[i]) {
return true;
}
}
return false;
}
@Override
public int indexOf(int toFind) {
for (int i = 0; i < usedSize; i++) {
if (toFind == elem[i]) {
return i;
}
}
return -1;
}
@Override
public int get(int pos) {
try{
checkPosInSetAndGet(pos);
}catch(PosNotLegalException e){
e.printStackTrace();
}
return elem[pos];
}
@Override
public boolean isEmpty() {
return usedSize == 0;
}
@Override
public void set(int pos, int value) {
try{
checkPosInSetAndGet(pos);
}catch(PosNotLegalException e){
e.printStackTrace();
}
elem[pos] = value;
}
@Override
public void remove(int key) {
if(!contains(key)){
System.out.println("key值不存在...");
return;
}
int delIndex = indexOf(key);
for(int i = delIndex+1;i<usedSize;i++){
elem[i-1] = elem[i];
}
usedSize--;
}
@Override
public int size() {
return usedSize;
}
@Override
public void clear() {
elem = null;
usedSize = 0;
}
}
Test(测试类)
public class Test {
public static void main(String[] args) {
IArrayList iArrayList = new MyArrayList();
iArrayList.add(1);
iArrayList.add(2);
iArrayList.add(3);
iArrayList.add(4);
iArrayList.add(5);
iArrayList.display();
System.out.println("=====add(pos,data)=====");
iArrayList.add(2,6);
iArrayList.display();
System.out.println("=====contains======");
System.out.println(iArrayList.contains(10));
System.out.println(iArrayList.contains(6));
System.out.println("=====indexOf=====");
System.out.println(iArrayList.indexOf(5));
System.out.println(iArrayList.indexOf(10));
System.out.println("======set=====");
iArrayList.set(0,100);
iArrayList.display();
System.out.println("=====remove=====");
iArrayList.remove(3);
iArrayList.display();
iArrayList.clear();
}
}