在Java中构建一个bag类 [英] Building a bag class in Java

查看:1049
本文介绍了在Java中构建一个bag类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我急需这个项目的帮助。我试图为一个编程任务实现一个 Bag 类,我被挂在 addAll() Union() equals()



编辑:根据赋值, addAll()应该将第二个数组中的所有对象添加到第一。我不再遇到错误,当我运行它,但由于某种原因,它不会添加所有的元素从第二个数组,它将只添加第一个2.感谢的家伙,这一个是现在工作完美! p>

修改:对于 Union(),我应该创建第三个将包含前2个包的所有内容。在运行此方法时,我收到一个 ArrayIndexOutOfBoundsException 。我更新了biddulph.r之后的代码,它也工作得很好。再次感谢!



修改:首次尝试,对于 equals()它应该检查的行李的大小,以确保他们在大小相等,然后检查他们是否包含相同的数字。因为它现在写,我的 equals()方法将比较大小并返回布尔值,但我不知道如何使它比较实际值。

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

public class bag {
int maxSize = 10; //数组大小
int count = 0; //存储在数组中的项目数量
int [] a;
int [] b;
bag c;
bag d;

public bag(){
// for(int i = 0; i // a [i] = + 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 ){
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 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(da [i]);
}
}
}

public void union(bag c,bag d){
bag greater = new bag
for(int i = 0; i greater.add(c.a [i]);
}
for(int i = 0; count< d.size() - 1; i ++){
greater.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
if(c.union(c,d).contains(ca [i])& c.union(c,d).contains(da [i])){

return true;
}

}
return false;
}

}

}

我真的很感激任何帮助,你们可以给我,谢谢。



编辑:感谢大家的帮助,你们都是救生员。

解决方案

您的方法:

  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(da [i]);
}
}
}

面向对象编程。
请记住,方法addAll()已经作用于一个包,因此您不需要在参数中指定2个包。



调用示例:

  mybag.addAll(yourBag); 

会显示一个可能的用法 - 它会将yourBag的所有内容添加到myBag。



我给你这个方法是免费的(假设数组'a'包含了包的内容 - 我不知道,因为你的变量名不清楚) :

  public void addAll(Bag otherBag){
for(int i:otherBag.a){
add(i);
}
}

上述方法会将otherBag的所有内容复制到此包。



我注意到了另一件事 - 你也有ab []实例变量 - 是什么?
您还有2个其他包实例变量。不知道为什么。


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.

解决方案

Your method:

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

betrays your lack of understanding of Object Oriented programming. Remember that the method addAll() is already acting on a bag, and so you should not need to specify 2 bags in the arguments.

Calling example:

mybag.addAll(yourBag); 

would demonstrate a possible usage - it would add all contents of yourBag into myBag.

I'll give you this method for free (assuming that the array 'a' contains the contents of the bag - something I'm not sure about because your variable names aren't clear):

public void addAll(Bag otherBag) {
    for (int i : otherBag.a) {
        add(i);
    }
}

The above method will copy all contents of otherBag into this bag.

Another thing I noticed - you also have a b[] instance variable - what's that for? You also have 2 other bag instance variables. Not sure why.

这篇关于在Java中构建一个bag类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆