Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

collections - Building a bag class in Java

I'm in dire need of help with this project. I'm trying to implement a Bag class for a programming assignment, and I'm getting hung up on the addAll(), Union(), and equals(), methods.

Edit: According to the assignment, addAll() is supposed to add all of the the objects from the second array into the first. I'm no longer getting an error when I run it, but for some reason it will not add all of the elements from the second array, it will only add the first 2. Thanks guys, this one is working perfectly now!

Edit: For Union(), I'm supposed to create a third bag that will contain all the contents of the first 2 bags. I was getting an ArrayIndexOutOfBoundsException when running this method. I've updated the code following biddulph.r and it's also working great. Thanks again!

Edit: "First attempt" And for equals(), it's supposed to check the size of the bags to make sure they are equal in size, then check to see if they contain the same numbers. So as it's written now, my equals() method will compare sizes and return the boolean value for that, but I'm unsure of how to make it compare the actual values.

import java.util.Arrays;
import javax.swing.*;

public class bag {
  int maxSize = 10; //Size of the arrays
  int count = 0; //Number of items stored in the array
  int[] a;
  int[] b;
  bag c;
  bag d;

  public bag() {
    //for(int i = 0; i < maxSize; i++){
    //a[i] = (int)(1+Math.random()*100);
    //}
    a = new int[maxSize];
  }

  public String bagString() {
    return Arrays.toString(a);
  }

  public void add(int b) {
    try {
      a[count] = b;
      count++;
    } catch (ArrayIndexOutOfBoundsException n) {
      JOptionPane.showMessageDialog(null, "Array is full, element will not be added");
    }
  }

  public void removeRandom() {
    int i = (int)(1 + Math.random() * (count - 1));
    a[i] = a[count - 1];
    a[count - 1] = 0;
    count--;
  }

  public void remove(int b) {
    for (int i = 0; i < maxSize; i++) {
      if (contains(b)) {
        a[i] = a[count - 1];
      }
    }
  }

  public boolean isEmpty() {
    if (count == 0) return true;
    else return false;
  }

  public boolean contains(int b) {
    int tf = 0;
    for (int i = 0; i < maxSize; i++) {
      if (a[i] == b) tf = 1;
    }
    if (tf == 1) return true;
    else return false;
  }

  public int size() {
    return count;
  }

  public void addAll(bag c, bag d) {
    if (a.length >= c.size() + d.size()) {
      for (int i = 0; c.size() <= d.size(); i++) {
        c.add(d.a[i]);
      }
    }
  }

  public void union(bag c, bag d) {
    bag bigger = new bag();
    for (int i = 0; i < c.size(); i++) {
      bigger.add(c.a[i]);
    }
    for (int i = 0; count < d.size() - 1; i++) {
      bigger.add(d.a[i]);
    }
    System.out.println(bigger.bagString());
  }

      public boolean equals(bag c, bag d){

        if(c.size() != d.size()){

                return false;

        }else{

                for(int i = 0; i < c.union(c, d).size(); i++){

                        if(c.union(c, d).contains(c.a[i]) && c.union(c, d).contains(d.a[i])){

                        return true;                                   
                        }                              

                }              
                    return false;                              
        }

    }

}

I really appreciate any help you guys can give me, thanks.

EDIT: Thanks to everyone for your help, you guys are life savers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem for addAll() is here

   if (a.length >= c.size() + d.size()) {

        for (int i = 0; c.size() <= d.size(); i++) {
            c.add(d.a[i]);
        }
   }

You shouldn't be adding elements until your c bag becomes bigger than d, you should be adding all of d's elements to c.

for (int i = 0; i < d.size(); i++) {
    c.add(d.a[i]);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...