class MyArrayList6 { // Linked list implementation of ArrayList with size variable private Cell myList; private int mySize; public MyArrayList6() { myList=null; mySize=0; } public E get(int pos) { Cell ptr=myList; if(pos>=mySize) throw new IndexOutOfBoundsException(); for(int count=0; count ptr=myList; if(pos>=mySize) throw new IndexOutOfBoundsException(); for(int count=0; count(item,null); else { Cell ptr=myList; for(; ptr.next!=null; ptr=ptr.next) {} ptr.next = new Cell(item,null); } mySize=mySize+1; } public void add(int pos,E item) { if(pos>mySize) throw new IndexOutOfBoundsException(); if(pos==0) myList = new Cell(item,myList); else { Cell ptr=myList; for(int count=1; count(item,ptr.next); } mySize=mySize+1; } public E remove(int pos) { E item; if(pos>=mySize) throw new IndexOutOfBoundsException(); else if(pos==0) { mySize=mySize-1; item=myList.first; myList=myList.next; return item; } else { Cell ptr=myList; for(int count=1; count ptr=myList; for(; ptr.next!=null&&!item.equals(ptr.next.first); ptr=ptr.next) {} if(ptr.next==null) return false; else { mySize=mySize-1; ptr.next = ptr.next.next; return true; } } } public int size() { return mySize; } 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; } } }