DynamicArray
Ein Array bietet die Möglichkeit, Daten temporär zu speichern. Da jedoch ein Array immer eine feste Größe hat, können wir keine Elemente nachträglich hinzufügen.
Der Code unten bietet eine mögliche Lösung, ein Array dynamisch zu erweitern. Der Code ist jedoch nur als didaktisches Beispiel gedacht, da Java schon genug dynamische Collection-Klassen anbietet.
Das Beispiel enthält zusätzlich die statischen Methoden save/load zur dauerhaften Speicherung der Bean (oder irgendeiner anderen Bean).
Die Klasse "DynamicArrayDemo" zeigt ein Anwendungsbeispiel:
<code=java> import java.beans.*; import java.io.*;
public class DynamicArray {
private Object[] list; private int size; private int increase; public DynamicArray(){ this(10, 10); } public DynamicArray(final int initialCapacity){ this(initialCapacity, 10); } public DynamicArray(final int initialCapacity, final int expand){ setList(new Object[initialCapacity]); setIncrease(expand); } public void add(final Object element) { if (getSize() == list.length) { expand(); } list[size++] = element; } public Object get(final int index) { if (index > -1 && index < getSize()) { return list[index]; }else{ return null; } } private void expand() { Object[] tmp = new Object[list.length + getIncrease()]; System.arraycopy(list, 0, tmp, 0, list.length); setList(tmp); } public int getIncrease() { return increase; } public void setIncrease(final int increase) { if(increase <= 0){ throw new IllegalArgumentException("increase <= 0"); } this.increase = increase; } public Object[] getList() { return list; } public void setList(final Object[] list) { if(list == null){ throw new IllegalArgumentException("list == null"); } if(size > list.length){ setSize(list.length); } this.list = list; } public int getSize() { return size; } public void setSize(final int size) { if (size > list.length) { throw new IllegalArgumentException("size > " + list.length); } if (size < 0) { throw new IllegalArgumentException("size < 0"); } this.size = size; } @Override public String toString() { StringBuilder bld = new StringBuilder(16); bld.append("["); for (int i = 0; i < getSize(); i++) { bld.append(get(i).toString()); if(i < getSize() - 1){ bld.append(", "); } } bld.append("]"); return bld.toString(); } public static void save(final Object obj, final String filename) { try { XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename))); encoder.writeObject(obj); encoder.close(); } catch (IOException e) { e.printStackTrace(); } } public static Object load(final String filename) { Object obj = null; try { XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename))); obj = decoder.readObject(); decoder.close(); } catch (FileNotFoundException ex) { } return obj; }
} </code=java>
<code=java> import java.util.logging.Level; import java.util.logging.Logger;
public class DynamicArrayDemo {
private final String filename = "DynamicArray.xml";
public DynamicArrayDemo() { DynamicArray dynamicArray; Object load = DynamicArray.load(filename); if (load == null) { dynamicArray = new DynamicArray(10, 10); } else { dynamicArray = (DynamicArray) load; } int size = dynamicArray.getSize(); for (int i = size; i < size + 15; i++) { dynamicArray.add("Test_" + String.valueOf(i)); } Logger.getLogger("log").log(Level.INFO, dynamicArray.toString()); DynamicArray.save(dynamicArray, filename); }
public static void main(String[] args) { DynamicArrayDemo dynamicArrayDemo = new DynamicArrayDemo(); }
} </code=java>