how do i make arrays equal if they have to be filled?

71 Views Asked by At

i am confused because I need my array to be equal to the other array but I don't know how to compare them without losing their values

2

There are 2 best solutions below

1
JBurk94 On

If both roots are null you will get an undesired result based on what you're trying to do with your second if condition.

It looks like if both roots are null you want to return true, but you're returning false. You could use just one if statement

if(thisRoot == null || otherRoot == null){
  return thisRoot == null && otherRoot == null;
}

You have a bigger problem with how you're comparing the data of the two nodes.

thisRoot.getData() != otherRoot.getData()

This comparison is not what I think you're looking for. Instead you should overrride the equals method for your data objects and compare using it instead

2
S.L. Barth is on codidact.com On

The order of your conditions causes a problem.

if (thisRoot == null || otherRoot == null) {
    return false;
}
if (thisRoot == null && otherRoot == null) {
    return true;
}

The first condition will evaluate to true (and lead to return false) even if both branches are null. You should first evaluate if both branches are null; after that, you can check the case where only one of them is null.