2012-02-07 71 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