Lena Monika Marshall
Lena Monika Marshall

Reputation: 113

Why does my method not return false when I change the array's element?

I'm trying to check wvhether my 2D array is symmetric or not. I wrote a method to check if array is symmetric. It always returns true, even if I change elements in the input array. What am I doing wrong?

Here is my code:

public class learnigBoolean
{
    public static void main(String[] args)
    {
        int[][] array = {
            { 1,  1,  4, -1},
            { 1,  5,  0, -1},
            { 4,  0,  1, -4},
            {-1, -1,  4, 10}
        };

        System.out.println(symetrisk(array));
    }

    public static boolean symetrisk(int[][] f)
    {
        for (int out = 0; out < f.length; out++) {
            for (int in = 0; in < f[out].length; in++) {
                if (f.length == f[out].length && f[out][in] == f[out][in]) {
                    return true;
                }
            }
        }
        return false;
    }
}

Upvotes: 2

Views: 139

Answers (2)

f[out][in] == f[out][in] 

Will always return true. Also calling "return true" will exit the loop after the first positive match which is:

f[0][0] == f[0][0] 

also always true.

If you want to make it more efficient you might want to initialize your second loop to "out" to prevent checking the same pair twice, skip checking numbers against themselves and exit the loop as soon as you find a non-match like so:

public static boolean symetrisk(int[][] f)
{
    for (int out = 0; out < f.length; out++) {
        if (f.length == f[out].length) //only need to check this once per row.
        {
            for (int in = out + 1; in < f[out].length; in++) 
            {
                if (f[out][in] != f[in][out]) 
                {
                        return false; //once we find a non-matching value we stop checking
                }
            }
        } 
        else 
        {
            return false; //non-square array.
        }           
    }
    return true;
}

Upvotes: 2

A-y
A-y

Reputation: 793

if(f.length==f[out].length && f[out][in]==f[out][in])

The first check ensure your matrix is squared, the second does nothing! You are comparing each element to itself.

Don't you mean :

if(f.length==f[out].length && f[out][in]==f[in][out])

But your return statement is problematic, as stated by Michael Faisst.

You need something like this :

   for (int out = 0; out < f.length; out++) {
        for (int in = 0; in < f[out].length; in++) {
            if (f.length != f[out].length || f[out][in] != f[in][out])) {
                return false;
            }
        }
    }
    return true;

By inverting the check, you ensure that every element is checked before you return true.

Think of it this way : you only need to find one elements that doesn't satisfy the condition to say your array is not symmetric. However, you need to check every elements before you can say your array is symmetric.

You were doing the opposite, saying the array is symetric after only one check.

Upvotes: 5

Related Questions