2015-03-31 97 views
1

代碼MyArrayList類:製作/實現一個迭代器arraylists-的Java

public class MyArrayList implements Iterable<Object> { 
public static final int DEFAULT_SIZE = 5; 
public static final int EXPANSION = 5; 
private int capacity; 
private int size; 
private Object[] items; 
private int currentSize; 

public MyArrayList() { 
    size = 0; 
    capacity = DEFAULT_SIZE; 
    items = new Object[DEFAULT_SIZE]; 
    this.currentSize = items.length; 
} 

@Override 
public Iterator<Object> iterator() { 
    Iterator<Object> it = new Iterator<Object>() { 
     private int currentIndex = 0; 

     @Override 
     public boolean hasNext() { 
      return currentIndex < currentSize && items[currentIndex] != null; 
     } 

     @Override 
     public Object next() { 
      return items[currentIndex++]; 
     } 

     @Override 
     public void remove() { 
      throw new UnsupportedOperationException(); 
     } 

    }; 
    return it; 
} 


     private void expand() { 
      Object[] newItems = new Object[capacity + EXPANSION]; 
      for (int j = 0; j < size; j++) newItems[j] = items[j]; 
      items = newItems; 
      capacity = capacity + EXPANSION; 
     } 

     public void add(Object obj) { 
      try { 
       if (size >= capacity) this.expand(); 
       items[size] = obj; 
       size++; 
      } catch (IndexOutOfBoundsException e) { 
       System.out.println("There is an error adding this word." + e.getMessage()); 
      } 
     } 

     public int size() { 
      return size; 
     } 

     public Object get(int index) { 
      try { 
       return items[index]; 
      } catch (ArrayIndexOutOfBoundsException e) { 
       System.out.println("There is an error getting this word from position: " + e.getMessage()); 
      } 
      return items[index]; 
     } 


     public void add(int index, Object obj) { 
      try { 
       if (size >= capacity) this.expand(); 
       for (int j = size; j > index; j--) items[j] = items[j - 1]; 
       items[index] = obj; 
       size++; 
      } catch (IndexOutOfBoundsException e) { 
       System.out.println("There is an error adding this word to array at position: " + e.getMessage() + "."); 
      } 
     } 


     public boolean remove(Object obj) { 
      for (int j = 0; j < size; j++) { 
       if (obj.equals(this.get(j))) { 
        for (int k = j; k < size - 1; k++) items[k] = items[k + 1]; 
        items[size] = null; 
        size--; 
        return true; 
       } 
      } 
      return false; 
     } 

     public Object remove(int index) { 
      try { 
       Object result = this.get(index); 
       for (int k = index; k < size - 1; k++) items[k] = items[k + 1]; 
       items[size] = null; 
       size--; 
       return result; 
      } catch (IndexOutOfBoundsException e) { 
       System.out.print("There is an error removing this word from position " + e.getMessage()); 
      } 
      return null; 
     } 
} 

} 

代碼主要方法。 (添加數據)

public class adding{ 

static MyArrayList zoo = new MyArrayList() { 


public static void printZoo() { 
    System.out.print("The zoo now holds " + zoo.size() + " animals: "); 
    for (int j = 0; j < zoo.size(); j++) System.out.print(zoo.get(j) + " "); 
    System.out.println(); 
} 
public static void main(String[] args) { 

    String[] zooList = {"Cheetah", "Jaguar", "Leopard", "Lion", "Panther", "Tiger"}; 

    for (String x: zooList) zoo.add(x); 
    printZoo(); 

    System.out.printf("\nTesting the iterator\n>> "); 
    Iterator it = zoo.iterator(); 
    while (it.hasNext()) { 
     System.out.print(it.next() + " "); 
    } 
    System.out.println(); 

    System.out.printf("\nTesting the iterator again without resetting\n>> "); 
    while (it.hasNext()) { 
     System.out.print(it.next() + " "); 
    } 
    System.out.println(); 

    System.out.printf("\nTesting the iterator again after resetting\n>> "); 
    it = zoo.iterator(); 
    while (it.hasNext()) { 
     System.out.print(it.next() + " "); 
    } 
    System.out.println(); 

    System.out.printf("\nTesting for-each loop\n>> "); 
    for(Object animal: zoo) System.out.print(animal + " "); 
    System.out.println(); 

    System.out.println("\nLetting all the animals escape"); 
    while (zoo.size()>0) zoo.remove(0); 
    printZoo(); 

    System.out.printf("\nTesting the iterator with an empty list\n>> "); 
    it = zoo.iterator(); 
    while (it.hasNext()) { 
     System.out.print(it.next() + " "); 
    } 
    System.out.println(); 

    System.out.println("\nTest complete"); 


} 
} 

所以我需要做一個正確的迭代器,因此它可以打印出來使用,而環路的ArrayList的內容。

輸出

The zoo now holds 6 animals: Cheetah Jaguar Leopard Lion Panther Tiger 

Testing the iterator 
>> Cheetah Jaguar Leopard Lion Panther //Works fine 

Testing the iterator again without resetting 
>> // This is still blank 

Testing the iterator again after resetting 
>> Cheetah Jaguar Leopard Lion Panther 

Testing for-each loop 
>> Cheetah Jaguar Leopard Lion Panther // Works fine. 

Letting all the animals escape 
The zoo now holds 0 animals: //Is there a way to remove by changing the MyArraylist class instead of changing the added class? 

Testing the iterator with an empty list 
>> Tiger //Still inaccurate. 

很肯定從MyArrayList類我迭代器的邏輯是不準確的。

回答

1

通過使用

static MyArrayList zoo = new MyArrayList() { 
     @Override 
     public Iterator<Object> iterator() { 
      return null; 
     } 
    }; 

聲明它會覆蓋在MyArrayList定義iterator方法一個新的匿名內部類。因此,只要構建動物園

static MyArrayList zoo = new MyArrayList(); 

,它應該是罰款(除了擴展方法,在您發佈的片段缺失)

+0

啊,忘了刪除那個額外的部分。非常感謝。現在由於某種原因,它不能正確迭代100%。我將發佈輸出。 (添加擴展btw) – bob9123 2015-03-31 02:05:46

+0

我添加了更改以希望解決這些問題 – 2015-03-31 02:20:43

+0

感謝您的答覆。請參閱編輯 – bob9123 2015-03-31 02:29:56

0

你只覆蓋Iterable<Object>界面在主類,它返回一個空迭代器。

更改代碼

static MyArrayList zoo = new MyArrayList() { 
@Override 
public Iterator<Object> iterator() { 
    return null; 
}}; 

static MyArrayList zoo = new MyArrayList(); 
0

嗯..這不正是它應該做的。

您已經聲明瞭動物園(Adding.java第7-12行),用空返回覆蓋了iterator()方法。

因此,迭代器爲null,並且只要嘗試訪問迭代器的方法,Java就會拋出NullPointerException。

2件小事情要注意。請提供所有方法(缺少expand())並遵循名稱召集(帶有大寫字母的類名)。

+0

好的,謝謝整理一切。出現了另一個問題,我添加到描述中 – bob9123 2015-03-31 02:10:01