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