views:

222

answers:

6

Im trying to create a method that take 2 int array as the input parameter and returns true if the array are reverse and false otherwise. This is what I have so far but it is wrong.

public static void main(String[] args)
{
    int b,a;
    int[] data1 = {14,-70,-18,88,85,97,-65,13,-71,-12};
    int[] data2 = {-12,-71,13,-65,97,85,88,-18,-70,14};
    boolean check = true;

    for (a=0;a<data1.length;a++)
    {
      for (b=data2.length-1;b>=0;b=b-1)
      {
                  if (data1[a] != data2[b])
                      check=false 
      }
    }
    System.out.println(check);
}

My example is suppose to print true but it doesn't.I am assuming the 2 arrays are of the same length. Can anyone help?

+1  A: 

You can do it in one loop, you don't need two.

for (int i=0,j=end;i<end;i++,j--)

Byron Whitlock
+2  A: 

in this, both array length should be equal. then

for(int i=0,j=array.length;i<array.length,j=0;i++,j--){
  write your comparison logic here
}
coder
You only need one variable - and your condition is somewhat odd as it includes an assignment...
Jon Skeet
oo, i'm new to java so I haven't learn that yet. Thanks for the help! :)
Dan
+5  A: 

You don't need two loops - you only need to loop once, using the index "normally" in one array, and from the other end for the other array:

public static boolean checkReversed(int[] x, int[] y)
{
    // For production code, possibly add nullity checks here (see comments)
    if (x.length != y.length)
    {
        return false;
    }
    // Loop through x forwards and y backwards
    for (int i = 0; i < x.length; i++)
    {
        if (x[i] != y[y.length - 1 - i])
        {
            // As soon as we've found a "mistake" we can exit:
            // This is simpler (IMO) than keeping a "check" variable
            return false;
        }
    }
    return true;
}
Jon Skeet
i see. Thank you for your advice!
Dan
Why would you add nullity checks? I would want an exception to be raised if I passed in a null value for one of the arrays.
Gabe
@gabe: True, for Java. In C# I'd want to throw ArgumentNullException instead of waiting for NullReferenceException. I keep forgetting that Java doesn't have that, annoyingly :( It's possible that if they're *both* null you might want to return true though.
Jon Skeet
@gabe - agreed. The code as written will throw a `NullPointerException` if either argument is `null`. This is (IMO) the best diagnostic for the problem, and it is consistent with the behaviour of most of the standard Java libraries.
Stephen C
@Stephen C: Yup - although only because Java doesn't have a more appropriate exception. An exception which explicitly said, "Argument x was null and shouldn't have been" would be better. Too late now though.
Jon Skeet
+3  A: 

You can try doing:

// compare the length.
check = (data1.length != data2.length)?false:true;

// if lengths are equal..go ahead and compare elements in reverse.
if(check) {    
    for(int i=0,j=data2.length;(i<data1.length) && (j>=0);i++,j--) {
        // if you find a mismatch..set check to false..and break
        // no need to compare other ele.
        if(data1[i] != data2[j]) {
            check = false;
            break;
        }
    }
}
codaddict
+2  A: 

Your code actually compares every element in data1 with every element with data2 and prints false if there is any one mismatch. That is not what you intend it to do.

Kai Chan
+1  A: 

Hi Dan, here is an answer to your question in a complete .java file

//yeah.java
public class yeah {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int[] data1 = {14,-70,-18,88,85,97,-65,13,-71,-12};
        int[] data2 = {-12,-71,13,-65,97,85,88,-18,-70,12};

        System.out.println(isReverse(data1, data2));
    }

    public static boolean isReverse(int[] a, int[] b)
    {
        if (a.length != b.length)   //If a and b are not of the same length how can they be reverse?
            return false;
        for (int i=0;i<a.length;i++)
            if (a[i] != b[a.length-i-1])
                return false;
        return true;
    }

}

Just a quick note about method and functions.. As soon as you discover that they are not reversed, you should exit using a return statement.. no need to keep on computing..

matdumsa
+1: Good approach.
Jim Ferrans