简体   繁体   English

实现一个 equals() 方法来比较 java 中用整数填充的两个对象中的内容

[英]implementing an equals() method to compare contents in two objects filled with integers in java

I keep getting "false" outputs for all tests, but I'm not sure where in the equals() method my mistake is.我不断得到所有测试的“错误”输出,但我不确定我的错误在 equals() 方法中的哪个位置。

I know I have to check each object to make sure they're the same size and then if they are, the elements inside have to be checked to see if they're the same or not.我知道我必须检查每个 object 以确保它们的大小相同,然后如果它们是相同的,则必须检查内部的元素以查看它们是否相同。 I thought that was what I was doing since the size() method returns manyItems and manyItems is the number of elements.我认为这就是我正在做的事情,因为 size() 方法返回 manyItems 而 manyItems 是元素的数量。 Unless I should be using data.length in the for loop?除非我应该在 for 循环中使用 data.length ?

I should be getting false, false, true, but instead, my output is false, false, false.我应该得到假,假,真,但相反,我的 output 是假的,假的,假的。

See code below (the equals() method and main method for testing are at the end of the code):见下面的代码(用于测试的equals()方法和main方法在代码的最后):

//implementation of IntArrayBag copied with equals method added to it
public class intArrayBag implements Cloneable {

    private int[] data;
    private int manyItems; 

    //initialize an empty bag with an initial capacity of 10
    //postcondition: this bag is empty and has an initial capacity of 10
    //throws: OutOfMemoryError - indicates insufficient memory for new int[10]
    public intArrayBag() {
        
        final int INITIAL_CAPACITY = 10;
        manyItems = 0;
        data = new int[INITIAL_CAPACITY];
   
    }
      
    //add new elements to this bag
    //parameters: elements - one or more new elements that are being inserted
    //postcondition: a new copy of the element has been added to this bag
    //throws: OutOfMemoryError - indicates insufficient memory for increasing the bag's capacity
    public void addMany(int... elements) {
    
        //if new elements were to take this bag beyond its current capacity, the capacity is increased before adding the new elements
        if (manyItems + elements.length > data.length) {
            //ensure twice as much space as needed
            ensureCapacity((manyItems + elements.length)*2);
      
        }
        System.arraycopy(elements, 0, data, manyItems, elements.length);
        manyItems += elements.length;
   
    }

    //determine the number of elements in this bag
    //returns: number of elements in this bag
    public int size() {
        return manyItems;
        
    }
  
    //equals method to compare bags
    //parameters: b - new bag to compare
    //returns: if b and the bag that activates this method have exactly the same elements, then return true otherwise the method returns false
    public boolean equals(intArrayBag b) {
        boolean isEqual = false;
        intArrayBag testBag = new intArrayBag();
        
        if (b.size() == testBag.size()) {
            for (int i = 0; i < b.size(); i++) {
                if (b.data[i] == testBag.data[i]) {
                    isEqual = true;
                    
                } else {
                    isEqual = false;
                    break;
                    
                }
                
            }
            
        } else {
            isEqual = false;
            return isEqual;
        
        }
        return isEqual;
            
    }
    
    //main method, used for testing equals method
    public static void main(String[] args) {
        intArrayBag bag1 = new intArrayBag();
        bag1.addMany(8, 9, 5, 2, 7, 3, 0, 1, 6);
        
        intArrayBag bag2 = new intArrayBag();
        bag2.addMany(5, 7, 6, 1, 0, 6, 7, 3, 9);
        
        //comparison test should return false
        System.out.println("comparison test1: " + bag1.equals(bag2));
        
        intArrayBag bag3 = new intArrayBag();
        bag3.addMany(2);
        
        intArrayBag bag4 = new intArrayBag();
        bag4.addMany(2, 5, 9);
        
        //comparison test should return false
        System.out.println("comparison test2: " + bag3.equals(bag4));
        
        intArrayBag bag5 = new intArrayBag();
        bag5.addMany(1, 2, 3);
        
        intArrayBag bag6 = new intArrayBag();
        bag6.addMany(1, 2, 3);
        
        //comparison test should return true
        System.out.println("comparison test3: " + bag5.equals(bag6));
        
    }
   
}

Your algorithm will work if instead of the testBag ( intArrayBag testBag = new intArrayBag(); ) you use the keyword this , here you're actually creating a new variable with 0 element and comparing that to the parameter.如果您使用关键字this而不是 testBag ( intArrayBag testBag = new intArrayBag(); ),您的算法将起作用,在这里您实际上是在创建一个具有 0 元素的新变量并将其与参数进行比较。

Also, you can do exactly the same thing with much shorter:此外,你可以用更短的时间做同样的事情:

public boolean equals(intArrayBag b) {
    if (b.size() != this.size()) {
        return false;
    }
    
    for (int i = 0; i < b.size(); i++) {
        if (b.data[i] != this.data[i]) {
            return false;
        }
    }
        
    return true;
}

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

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