DynamicArray: Unterschied zwischen den Versionen

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen
(Die Seite wurde neu angelegt: „Ein Array bietet die Möglichkeit, Daten temporär zu speichern. Da jedoch ein Array immer eine feste Grösse hat, können wir keine Elemente nachträglich hinzuf...“)
 
Zeile 70: Zeile 70:
 
         bld.append("]");
 
         bld.append("]");
 
         return bld.toString();
 
         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>

Version vom 24. April 2009, 23:51 Uhr

Ein Array bietet die Möglichkeit, Daten temporär zu speichern. Da jedoch ein Array immer eine feste Grösse 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. <code=java>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(size > list.length){
           setSize(list.length);
       }
       this.list = list;
   }
   public int getSize() {
       return size;
   }
   public void setSize(final int size) {
       if(size < 0 || size > list.length){
           throw new IllegalArgumentException("size > " + list.length);
       }
       this.size = size;
   }
   public String toString() {
       StringBuffer bld = new StringBuffer();
       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>