1 2 public class Main { 3 public static void main(String[] args) { 4 Main main = new Main(); 5 main.setReadOnly(false); 6 main.add(main); 7 } 8 9 private boolean readOnly; 10 private int size = 0; 11 private Object[] elements = new Object[1]; 12 public void add(Object element){ 13 if(readOnly){ 14 return; 15 } 16 if(atCapacity()) 17 grow(); 18 addElement(element); 19 } 20 private void addElement(Object element) { 21 elements[size++] = elements; 22 } 23 private void grow() { 24 Object[] newElements = new Object[elements.length+10]; 25 for(int i = 0;i < size; i++) 26 newElements[i] = elements[i]; 27 elements = newElements; 28 } 29 private boolean atCapacity() { 30 int newSize = size + 1; 31 if(newSize > elements.length){ 32 return true; 33 } else { 34 return false; 35 } 36 } 37 public void setReadOnly(boolean readOnly) { 38 this.readOnly = readOnly; 39 } 40 }