Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: code to check two int arrays to see if mirrors of each other

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default code to check two int arrays to see if mirrors of each other

    Trying to make a method that takes in two int arrays then reports true if they are mirror images of each other, or false if they are not. Assuming both arrays are of the same length.

    example:
    data1:{1,2,3}
    data2:{3,2,1}
    ==> true

    data1:{1,2,3}
    data2:{2,3,1}
    ==> false

    everything I try is giving me an ArayIndexOutOfBoundsException with my "for loop".
    This is the most recent code I tried:

      public boolean mirrorImage(int[] data1, int[] data2){
        boolean mirrors = true;
        int x = 0;
        int a = 0;
        int b = data2.length;
        while(x < data1.length){
          if(data1[a] != data2[b]){
            mirrors = false;
            break;
          }
          else{
            a = a + 1;
            b = b - 1;
          }
          x = x + 1;
        }
        return mirrors;
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: code to check two int arrays to see if mirrors of each other

    Hello ksahakian21!
    You should always print the variables which cause an exception to see what is going on. In your case it is quite obvious why you get an ArrayIndexOutOfBoundsException in your while loop. You assign data2.length to b and then you try to find data2[b]. Remember that arrays' indexes start at 0 and go to array.length-1.
    For example a 10-length array goes from 0-9.
    Hope I was clear.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    ksahakian21 (May 8th, 2012)

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Can Somebody check this code?
    By manager00104 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: January 30th, 2012, 11:27 AM
  3. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 07:43 PM
  4. Please check the code for the errors
    By nrao in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 16th, 2010, 05:37 PM
  5. Check this code out
    By jwill22 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 11th, 2010, 09:34 PM