2012-02-07 110 views
2

我在理解我的任務時遇到了一些困難,我只是想確保自己正確地做到了這一點,並希望在我的代碼中獲得另一雙眼睛。我的任務如下:在Java中使用數組實現Bag類/使用數組

使用Array作爲基礎數據結構來實現一個Bag類,我已經完成了這個工作。在我們的UML圖中,我的導師顯示它是一個對象數組,我很困惑我應該如何用對象來做這件事,以及如何比較它們。我創建了一個Node類作爲對象,並將其附加到代碼的末尾。我主要的問題是我不知道如何處理聯合和包含,因此導致我質疑我的代碼的其餘部分。

public class Bag extends Node { 

    public Node array[]; 
    public Node header = new Node(null, null, null); 

    public int bagSize = 10000; // An Initial size of array for the Objects in 
    // the bag 

    public int MAX_SIZE = 1000000; // Max Size of elements in a undetermined 
    // size bag 

    static int count = 0; // Number of Elements currently in Bag 

    // Constructor for base Bag 
    // This bag has a maximum size 
    // bag that a bag can have 
    public Bag() { 
     array = new Node[MAX_SIZE]; 
     bagSize = MAX_SIZE; 
     array[count] = header; 

    } 

    // Constructor for bag of size 
    // which user can input 
    public Bag(int size) { 
     array = new Node[size]; 
     bagSize = size; 
     array[count] = header; 
    } 

    // Method which a user can add objects 
    // to the bag, the count will go up each 
    // time the method is called on the object. 
    public void add(Node newNode) { 
     int numOperations = 0; 
     Node n = new Node(); 
     numOperations++; 
     n = newNode; 
     numOperations++; 
     count++; 
     numOperations++; 
     array[count] = n; 
     numOperations++; 
     System.out.println("Operations = " + numOperations); 
    } 

    /** Remove a random Node from the bag **/ 
    public void removeRandom() { 

    } 

    /** Remove a specified Node from the bag **/ 
    public void remove(Node obj) { 
     int numOperations = 0; 
     int i; 
     numOperations++; 
     for (i = 0; i <= array.length - 1; i++) { 
      if (array[i] == obj) { 
       int pos = i; 
       numOperations++; 
       for (int j = i; j <= array.length - 1; j++) { 
        array[i] = array[i + 1]; 
        numOperations++; 
        if (i + 1 == array.length) 
         break; 
        numOperations++; 
       } 
       break; 
      } 
     } 

    } 

    /** Check is bag is empty **/ 
    public boolean isEmpty() { 
     System.out.println("Operations = 1"); 
     return (count == 0); 
    } 

    /** Check if bag contains the Node **/ 
    public boolean contains(String data) { 
     boolean contain = false; 
     if (!isEmpty()) { 
      for (int i = 0; i <= count; i++) { 
       if (data == array[i].data) { 
        return contain = true; 
       } else { 
        return contain = false; 
       } 
      } 
     } 
     return contain; 
    } 

    /** Return the size of bag **/ 
    public int size() { 
     return count; 
    } 

    /** Add all Nodes of bag a to the specified bag **/ 
    public static void addAll(Bag b, Bag a) { 
     int numOperations = 0; 
     if (b.bagSize >= a.size() + b.size()) { 
      numOperations++; 
      for (int i = 0; i <= a.size(); i++) { 
       b.add(b.array[i]); 
       numOperations++; 
      } 
     } 

    } 

    /** 
    * Join all elements of the two bags into a new bag but without any 
    * overlapping items. i.e No Duplicates 
    */ 
    public static Bag union(Bag a, Bag b) { 
     Bag bigger = new Bag(a.size() + b.size()); 
     if(!a.isEmpty() && !b.isEmpty() && a.equals(b)){ 
      for(int i=0;i<=bigger.bagSize;i++){ 
       if(a.contains(a.getData()) && b.contains(b.getData())){ 
        bigger.add(b.getNext());  
       }else{ 
        bigger.add(a.getNext()); 
        bigger.add(b.getNext()); 
       } 
      } 
     } 
     return b; 
    } 

    /** Determine if the bags equal each other in items **/ 
    public boolean equals(Bag a) { 
     if(bagSize == a.size()){ 

     } 
     return false; 
    } 
} 

public class Node { 
    String data; 
    Node prev,next; 

    public Node(Node next, Node prev, String data){ 
     this.next = next; 
     this.prev = prev; 
     this.data = data; 
    } 

    public Node(){ 

    } 

    public String getData() {return data;} 

    public Node getPrev() { return prev;} 

    public Node getNext() {return next;} 

    public void setData(String newName) {data = newName;} 

    public void setPrev(Node newPrev) { prev = newPrev; } 

    public void setNext(Node newNext) { next = newNext;} 

} 
+1

你對包含和聯合方法有什麼不瞭解? contains()方法檢查包中是否存在元素。聯合的定義也很清楚([參見Wikipedia](http://en.wikipedia.org/wiki/Union_(set_theory))) – wonderb0lt 2012-02-07 15:25:37

+1

爲什麼使用鏈接節點(從而製作某種鏈接列表),因爲你被要求使用一個數組來存儲你的對象。你不需要一個Node類。 Bag當然不是Node,所以它不能擴展Node。 – 2012-02-07 15:29:25

+0

它現在很有意義,我剛剛完成了LinkedLists的任務,所以我只是在那個思維模式中,但現在我已經明白了,謝謝,並且我知道聯盟是什麼,但這只是讓我感到困惑,因爲我將鏈接列表我正在嘗試做什麼。現在已經清理好了,所以我應該好好去。 – sealsix 2012-02-07 16:19:00

回答

1

你不需要Node類,你的包是由數組支持,創建一個鏈表是多餘的。

您的contains似乎是在正確的軌道上(但還沒有),它可能應該使用equals()而不是==。不過,您需要重新檢查如何處理contain標誌。

與工會有什麼問題?你能描述你是如何實現它的嗎(代碼不完全清楚)?

+0

謝謝,我使用了一個Node類,因爲我很困惑如何將對象添加到我的包中。我們應該使用對象,然後對其中的數據進行比較和處理,這就是爲什麼我首先使用了節點,但是我會改變它。至於聯盟,我只是對它應該如何完成感到困惑。我的教練說應該在兩個循環中完成,但我不知道從哪裏來。我想我只是難以比較數據,以確保沒有副本,然後將它們添加到一個新的包。 – sealsix 2012-02-07 16:00:15

+0

那麼,你已經有了一個'contains()'方法,看起來好像在這裏很有用。你從兩個包開始,這可能是你需要兩個循環的原因。 – Dmitri 2012-02-07 16:31:36