All pastes #267435 Raw Edit

Someone

public java v1 · immutable
·published 2006-12-04 21:28 UTC
rendered paste body
public class ArrayList implements List {	private String[] elements;	private int currPosition = 0;	public ArrayList() {		elements = new String[20];	}	private void exchange() {		String temp;		for (int i = currPosition + 1; i < elements.length; i++) {			if (elements[i] == null) {				currPosition = i - 1;				break;			}			temp = elements[i];			elements[i] = null;			elements[i - 1] = temp;			if (i == elements.length - 1) {				currPosition = i;				break;			}		}	}	private void enlargeArray() {		String[] temp = new String[elements.length + 20];		for (int i = 0; i < elements.length; i++) {			temp[i] = elements[i];		}		elements = temp;	}	public void add(String element) {		if (currPosition > elements.length) {			enlargeArray();		}		elements[currPosition] = element;		currPosition++;	}	public void clear() {		elements = new String[20];		currPosition = 0;	}	public boolean contains(String element) {		for (int i = 0; i < elements.length; i++) {			if (elements[i] != null) {				if (elements[i].equals(element)) {					return true;				}			}		}		return false;	}	public ListIterator getIterator() {		ArrayListIterator iterator = new ArrayListIterator();		return iterator;	}	public int getSize() {		return currPosition;	}	public int indexOf(String element) {		for (int i = 0; i < currPosition; i++) {			if (elements[i].equals(element)) {				return i;			}		}		return -1;	}	public void insert(String element, int index) {		if (index <= currPosition) {			elements[index] = element;		}	}	public boolean isEmpty() {		if (currPosition == 0 && elements[0] == null) {			return true;		}		return false;	}	public void remove(String element) {		for (int i = 0; i < currPosition; i++) {			if (elements[i].equals(element)) {				elements[i] = null;				if (currPosition - 1 > i) {					currPosition = i;					exchange();					return;				} else {					currPosition = i;					return;				}			}		}	}	public void removeElementAt(int index) {		if (index < currPosition) {			elements[index] = null;			if (index == currPosition - 1) {				currPosition = index;			} else {				exchange();			}		}	}	public class ArrayListIterator implements ListIterator {		private int currIndex = 0;		public boolean hasNext() {			if (currIndex + 1 <= currPosition) {				return true;			}			return false;		}		public String next() {			currIndex++;			return elements[currIndex - 1];		}	}}