public class Myarraylist<e> {private int capacity = 10; private int size = 0; Private e[] values = NULL; @SuppressWarnings ("unchecked") public myarraylist () {values = (e[]) new object[capacity]; } @SuppressWarnings ("Unchecked") public myarraylist (int capacity) {this.capacity = capacity; Values = (e[]) new object[this.capacity]; } public void put (E e) {if (E = = null) {throw new RuntimeException ("The value should not is Null. ");} if (size >= capacity) {enlargecapacity (); } Values[size] = e; size++; Public E get (int index) {if (index >= size) {throw new RuntimeException ("The Index:" + Index + "is out of band."); } return Values[index]; } public void Remove (int index) {if (index >= size) {throw new RuntimeException ("The index: "+ index +" is out of band. "); for (int i = index; i < size-1; i++) {Values[i] = values[i + 1]; } values[size-1] = null; size--; } @SuppressWarnings ("Unchecked") private void enlargecapacity () {capacity = capacity * 2; E[] Tmpvalues = (e[]) new object[capacity]; System.arraycopy (values, 0, tmpvalues, 0, size); values = tmpvalues; The public String toString () {StringBuilder sb = new StringBuilder (); Sb.append ("["); for (int i = 0; i < size; i++) {sb.append (Values[i]). Append (","); } if (Size > 0) {sb.deletecharat (Sb.length ()-1); } sb.append ("]"); return sb.tostring (); }/** * @param args */public static void main (string[] args) {myarraylist<integer> MyList = new myarraylist<integer> (); for (int i=0;i<1000;i++) {mylist.put (i); } System.out.println (Mylist.tostring ()); } }
Self-made data structures (containers)-java development with the most ArrayList and HashMap