class MyArrayList5 { // Linked list implementation of ArrayList private Cell myList; public MyArrayList5() { myList=null; } public E get(int pos) { Cell ptr=myList; for(int count=0; count ptr=myList; for(int count=0; count(item,null); else { Cell ptr=myList; for(; ptr.next!=null; ptr=ptr.next) {} ptr.next = new Cell(item,null); } } public void add(int pos,E item) { if(pos==0) myList = new Cell(item,myList); else { Cell ptr=myList; for(int count=1; ptr!=null&&count(item,ptr.next); } } public E remove(int pos) { E item; if(myList==null) throw new IndexOutOfBoundsException(); else if(pos==0) { item=myList.first; myList=myList.next; return item; } else { Cell ptr=myList; for(int count=1; ptr.next!=null&&count ptr=myList; for(; ptr.next!=null&&!item.equals(ptr.next.first); ptr=ptr.next) {} if(ptr.next==null) return false; else { ptr.next = ptr.next.next; return true; } } } public int size() { int count=0; for(Cell ptr=myList; ptr!=null; ptr=ptr.next, count++) {} return count; } public int indexOf(E item) { int count=0; Cell ptr=myList; for(; ptr!=null&&!item.equals(ptr.first); ptr=ptr.next, count++) {} if(ptr==null) return -1; else return count; } public int lastIndexOf(E item) { int pos=-1; Cell ptr=myList; for(int count=0; ptr!=null; ptr=ptr.next, count++) if(item.equals(ptr.first)) pos=count; return pos; } public String toString() { String str="["; if(myList!=null) { str+=myList.first; for(Cell ptr=myList.next; ptr!=null; ptr=ptr.next) str+=","+ptr.first; } return str+"]"; } private static class Cell { T first; Cell next; Cell(T h,Cell t) { first=h; next=t; } } }