public static void main(String[] args) { AbstractList<Integer> list = new ConcreteArrayList<Integer>(10); // Ein paar werte weren in die Liste gelegt for (int i = 0; i < 5; i++) { try { list.insert(i, i); } catch (IndexOutOfBoundsException error) { error.printStackTrace(); } } System.out.println(); // Die Liste wir in der Console angezeigt for (int i = 0; i < list.size(); i++) { try { System.out.print(list.retrieve(i)); if (i + 1 < list.size()) System.out.print(" - "); } catch (IndexOutOfBoundsException error) { error.printStackTrace(); } } System.out.println(); // Ein neue Objekt wird in die Liste an der Stelle 2 gelegt try { list.insert(2, 3); } catch (IndexOutOfBoundsException error) { error.printStackTrace(); } for (int i = 0; i < list.size(); i++) { try { System.out.print(list.retrieve(i)); if (i + 1 < list.size()) System.out.print(" - "); } catch (IndexOutOfBoundsException error) { error.printStackTrace(); } } System.out.println(); // AbstrackList.find(int pos) liefert -1 wenn er nichts gefunden hat. // Man könnte schon prüfen ob index gültig ist. Es gibt aber einen Fehler // in der Implementierung von find(), Als Übung du könntest diesen Fehler suchen // und korrigieren :) int index = list.find(4); try { System.out.println(" Objekt gefunden : " + list.retrieve(index)); } catch (IndexOutOfBoundsException error) { error.printStackTrace(); } System.out.println(); /* * Das Objekt mit dem index 2 wird von der Liste entfernt. */ try { list.delete(2); } catch (IndexOutOfBoundsException error) { error.printStackTrace(); } System.out.println("Iterable :"); for (Integer i : list) { System.out.print("- " + i); } System.out.println("\n Not Iterable :"); for (int k = 0; k < list.size(); k++) { try { System.out.print(list.retrieve(k)); if (k + 1 < list.size()) System.out.print(" - "); } catch (IndexOutOfBoundsException error) { error.printStackTrace(); } } }
public Object getValue(Object key) { int pos = indexOf(key); if (pos >= 0) { if ((flag & MAP) == MAP) { return this.getByIndex(SMALL_VALUE, pos + index, size); } return this.getByIndex(SMALL_KEY, pos, size); } if (!(key instanceof String)) { return null; } String keyString = "" + key; int len = 0; int end = 0; int id = 0; for (; len < keyString.length(); len++) { char temp = keyString.charAt(len); if (temp == '[') { for (end = len + 1; end < keyString.length(); end++) { temp = keyString.charAt(end); if (keyString.charAt(end) == ']') { end++; break; } else if (temp > 47 && temp < 58 && id >= 0) { id = id * 10 + temp - 48; } else if (temp == 'L') { id = -2; } } if (end == keyString.length()) { end = 0; } break; } else if (temp == '.') { end = len; id = -1; break; } } if (end == 0 && len == keyString.length()) { id = -1; } Object child = getByIndex(SMALL_VALUE, indexOf(keyString.substring(0, len)) + this.index, size); if (child != null) { if (end == 0) { if (id >= 0 || id == -2) { if (child instanceof AbstractList<?>) { AbstractList<?> list = (AbstractList<?>) child; if (id == -2) { id = list.size() - 1; } if (list.size() >= id) { return list.get(id); } } } else { return child; } } else { if (id >= 0 || id == -2) { if (child instanceof AbstractArray) { if (end == len + 2) { // Get List BaseItem result = this.getNewList(true); AbstractList<?> items = (AbstractList<?>) child; for (int z = 0; z < items.size(); z++) { result.with( ((AbstractList<?>) items.get(z)).getValue(keyString.substring(end + 1))); } return result; } AbstractList<?> list = (AbstractList<?>) child; if (id == -2) { id = list.size() - 1; } if (list.size() >= id) { return ((SimpleKeyValueList<?, ?>) list.get(id)) .getValue(keyString.substring(end + 1)); } } } else if (child instanceof SimpleKeyValueList<?, ?>) { return ((SimpleKeyValueList<?, ?>) child).getValue(keyString.substring(end + 1)); } } } return null; }