简体   繁体   中英

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.

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. I thought that was what I was doing since the size() method returns manyItems and manyItems is the number of elements. Unless I should be using data.length in the for loop?

I should be getting false, false, true, but instead, my output is false, false, false.

See code below (the equals() method and main method for testing are at the end of the code):

//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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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