簡體   English   中英

Java /使用數組中的Bag類實現

[英]Bag Class Implementation in Java/Using Array

我在理解我的任務時遇到了一些困難,我只想確保自己做的正確,並希望在我的代碼上另辟pair徑。 我的任務如下:

我已經完成了使用Array作為基本數據結構實現Bag類的工作。 在我們的UML圖中,我的講師將其顯示為一個對象數組,並對我應該如何使用對象以及如何比較它們感到困惑。 我創建了一個Node類作為對象,並將其附加到代碼末尾。 我的主要問題是我不知道該為Union和Contains做什么,因此使我對其余的代碼提出疑問。

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;}

}

您不需要Node類,Bag由數組支持,創建鏈接列表是多余的。

您的contains似乎在正確的軌道上(但尚未到位),它可能應該使用equals()而不是== 但是,您需要重新檢查contain標志的處理方式。

工會有什么問題? 您能描述一下您如何嘗試實現它(代碼尚不完全清楚)?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM