简体   繁体   English

Java /使用数组中的Bag类实现

[英]Bag Class Implementation in Java/Using Array

I am having some difficulty understanding my assignment and I just want to make sure I am doing it correctly and would like to get another pair of eyes on my code. 我在理解我的任务时遇到了一些困难,我只想确保自己做的正确,并希望在我的代码上另辟pair径。 My assignment is as follows: 我的任务如下:

Implement a Bag class using an Array as the base data structure, which I have done. 我已经完成了使用Array作为基本数据结构实现Bag类的工作。 In our UML diagram my instructor shows it being an array of objects and am confused as to how I should be doing this with objects and how to compare them. 在我们的UML图中,我的讲师将其显示为一个对象数组,并对我应该如何使用对象以及如何比较它们感到困惑。 I created a Node class to act as the objects and will attach that to the end of code. 我创建了一个Node类作为对象,并将其附加到代码末尾。 I main problem is I don't know what to do for Union and contains and are therefore causing me to question the rest of my code. 我的主要问题是我不知道该为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;}

}

You don't need a Node class, your Bag is backed by an array, creating a linked list is redundant. 您不需要Node类,Bag由数组支持,创建链接列表是多余的。

Your contains seems to be on the right track (but not there yet), it should probably be using equals() instead of == . 您的contains似乎在正确的轨道上(但尚未到位),它可能应该使用equals()而不是== You need to re-check how the contain flag is handled, though. 但是,您需要重新检查contain标志的处理方式。

What's the issue you're having with union? 工会有什么问题? Can you describe how you're trying to implement it (the code isn't entirely clear)? 您能描述一下您如何尝试实现它(代码尚不完全清楚)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM