1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

.equals() method for Arrays (Java)

  1. Sep 30, 2015 #1
    1. The problem statement, all variables and given/known data
    Make 2 classes WrapperShallow and WrapperDeep.
    Each class is simply a wrapper class to hold a private array variable.
    int [] a;
    The default constructor for each class should initialize “a”.
    Each class should have a toString() and equals().
    Each class should have a setArray method that allows you to set the “a” variable.
    WrapperShallow should have an invalid copy constructor.
    public WrapperShallow(WrapperShallow ws){
    a = ws.a;
    }
    WrapperDeep should have a properly functioning copy constructor.
    public WrapperDeep(WrapperDeep ws){
    a = new int[3];
    for(int i = 0; i < 3; i++)
    a=ws.a;
    }
    Think about why shallow is wrong and deep is correct! What happens to the old “a” in the WrapperDeep copyconstructor? (think garbage collection)
    Example Output:
    --------------------
    Configuration: <Default>
    ---
    -----------------
    **** TESTING SHALLOW OBJECTS ****
    inital shallow object contains
    7 17 77
    copy shallow object contains
    7 17 77
    inital shallow object changed to
    13 14 15
    copy shallow object not changed contains
    13 14 15
    WOOPS! ws.equals(ws2) is true
    **** TESTING DEEP OBJECTS ****
    inital deep object contains
    2 3 4
    copy deep object contains
    2 3 4
    inital deep object changed to
    7 6
    -
    5
    copy deep object not changed contains
    2 3 4
    RIGHT! wd.equals(wd2) is false
    Process completed.

    3. The attempt at a solution
    I have the bulk of the wrapper classes done right according to the professor, but my .equals() method is completely wrong according to him. He left it to me to figure out, and I discovered that when it compares the arrays it's only checking the references of them and not the content of them. So when I comment out the setArray() method, even though the arrays are equal, it says they are not in the output.

    Searching for solutions have brought only one answer, that .equals() is not capable of testing the content of arrays, and you must use Arrays.equals from the Arrays utility to test the content. However, the syntax for doing this always requires two parameters in the equals() method as far as I've found.

    i.e.

    public boolean equals(Object[] a, Object[] b)

    He explicitly says in both his notes on my attempt of the assignment and in the example that he expects the equals method to have only one parameter, but I've tried every variation of equals with one parameter in my book and I know now that either he's tricking me (unlikely) or I am fundamentally misunderstanding what he's asking and the content of the book.

    Here's are my .java files for the project. The Wrapper main class is the test program. Just start me off on where I'm going wrong.
     

    Attached Files:

  2. jcsd
  3. Sep 30, 2015 #2
    Notice: A solution presented itself. Please delete.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: .equals() method for Arrays (Java)
  1. Arrays in Java (Replies: 3)

  2. Java class Arrays (Replies: 1)

Loading...