ArrayUnorderedList.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. public class ArrayUnorderedList<T> extends ArrayList<T> implements UnorderedListADT<T> {
  2. public ArrayUnorderedList() {
  3. super();
  4. }
  5. public ArrayUnorderedList (int initialCapacity) {
  6. super(initialCapacity);
  7. }
  8. public void addToFront (T element) {
  9. if (size() == list.length)
  10. expandCapacity();
  11. for (int scan=rear; scan > 0; scan--)
  12. list[scan] = list[scan-1];
  13. list[0] = element;
  14. rear++;
  15. }
  16. public void addToRear (T element) {
  17. if (size() == list.length)
  18. expandCapacity();
  19. list[rear] = element;
  20. rear++;
  21. }
  22. public void addAfter (T element, T target) {
  23. if (size() == list.length)
  24. expandCapacity();
  25. int scan = 0;
  26. while (scan < rear && !target.equals(list[scan]))
  27. scan++;
  28. if (scan == rear)
  29. throw new ElementNotFoundException ("list");
  30. scan++;
  31. for (int scan2=rear; scan2 > scan; scan2--)
  32. list[scan2] = list[scan2-1];
  33. list[scan] = element;
  34. rear++;
  35. }
  36. }