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 int newSize = size + 1; 15 if(newSize > elements.length){ 16 Object[] newElements = new Object[elements.length+10]; 17 for(int i = 0;i < size; i++) 18 newElements[i] = elements[i]; 19 elements = newElements; 20 } 21 elements[size++] = elements; 22 } 23 } 24 public void setReadOnly(boolean readOnly) { 25 this.readOnly = readOnly; 26 } 27 28 }