Results 1 to 13 of 13
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Basic array question

    This was the question but I am totally lost
    Create a method called mirrorImage, which takes two integer arrays as input parameters. You may assume that the two actual parameters have the same length. Your method should return true if the arrays are the reverse of each other. Otherwise mirrorImage should return false.
    Examples:

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

    data1:{1,2,3}
    data2:{2,3,1}
    ==> false
    They also gave me this

    I put this in, but it's messy and theres something wrong...

    public boolean mirrorImage (int[] data1, int[] data2) {

    boolean b = false;
    int j=0;
    for (j=0; j<=data2.length-1; j++){
    }
    for (int i = data1.length-1; i >= 0; i--)
    {
    if(data1[i]==data2[j]) {
    b = true;
    }else {
    b=false;
    }
    }
    return b;
    I get this error, it's telling me I'm pointing a place outside of the array or something
    runtime error (line 8, column 0):
    java.lang.ArrayIndexOutOfBoundsException: 10
    -> array.ArrayQuestions.mirrorImage()
    Any help please :D

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You need to declare your j int in the loop condition (which will make it local to just the loop and protect you from the mistake you're making) and nest those loops:
    Java Code:
    //int j=0;
    for (int j=0; j< data2.length; j++) // standard practice is to do j < array.length
    {
      for (int i = data1.length-1; i >= 0; i--)
      {
         if(...)
           //...
       }
    }
    I would also initialize the boolean true and then within the loop make it false and break out if a mismatch is found. don't set it to both true or false within your if / else statements, just make it false.

    Edit: another way is to just use a single loop:
    Java Code:
    for (int i=0; i< array1.length; i++)
    {
      if (array1[i] != array2[array1.length - i - 1]
      {
         // ...
      }
    }
    Last edited by Fubarable; 01-08-2009 at 05:10 AM.

  3. #3
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Thanks for your help, but I still have a small problem, I think it's returning true based just on the first number? I started arrays today so cut me some slack :D

    boolean b =false;
    for (int j=0; j< data2.length; j++)
    for (int i = data1.length-1; i >= 0; i--)
    {
    if(data1[i]==data2[j]) {
    b = true;
    }else{
    b=false;
    }

    }
    return b;
    On these inputs,

    the first input array:
    1 2 3

    the second input array:
    3 3 1
    Your code produced:
    true

    The correct solution produced:
    false

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) thanks for using code tags when posting code here, but also please use proper indentation (3-4 spaces is nice). This makes your code easier to read and is considered a nice way of showing respect and appreciation for those who are helping you.
    2) to find out why this is not working, move away from the computer and using pen and paper and two small arrays written out on the paper -- 3 items should do in each -- walk through your code mentally, think what will happen to your boolean variable each step of the way, and you'll see your error.

    Best of luck!

  5. #5
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Well I see the problem 2!=3... but the first and last match. I am confused now, how would I create enough booleans that equal to the length? Something like boolean a[] = new boolean[data2.length];
    But then I become confused on how I would make a for loop to check all of the booleans if I don't know how many of them will be created before hand? I would just create 3 booleans, but this program is smart and would try another calculation...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You only need one boolean variable, that's it. To find a solution, again you'll be best served to walk away from the computer and think of exactly how would you solve this on paper without a computer. Then use this logic to guide your programming. Again, come back if you're stuck, but remember, the harder you work, the more you'll learn.

    Edit: think also, do you need both an if and an else? Would either one or the other alone be enough here? If you start assuming that all are matched (that the boolean is true), and then you find a mismatch, do you have to do any more checking? In other words would it matter if all the rest matched or not? Or are you done with your checking?

    Again, best of luck!
    Last edited by Fubarable; 01-08-2009 at 05:57 AM.

  7. #7
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Gyah, I can't find of an easy fix solution. I would try and think about it more because I actually enjoy learning java except, it's just one class got loads of others. :(

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by jigglywiggly View Post
    Gyah, I can't find of an easy fix solution. I would try and think about it more because I actually enjoy learning java except, it's just one class got loads of others. :(
    Then walk away, and work on something else. Let it ferment in your mind, and come back to it in a bit. You may be surprised that you'll then be able to create a solution.

    Good luck.

  9. #9
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    I got it, I am a champion :D
    And thank you

    Java Code:
    boolean b =false;
    for (int i = 0; i<data1.length-1; i++){
      	if(data1[i]==data2[data1.length-1-i]) {
        		b = true;
        	}else{
      		b=false; 
                                                                                                                                                                        
      	}
                                                         
    }
    return b;

  10. #10
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Oh and how do I mark my thread as solved?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm afraid that it's not solved. Your boolean, b, will only equal the last evaluation performed. So if you have arrays
    {1, 2, 3} and {3, 3, 3}, since the pair of 3's will be compared last, this will be marked as mirror arrays. Again:

    * set your boolean to true at the beginning.

    * In your loop set it to false if a mismatch is found. Do not set it to true if a match is found. This is what I meant by when I said don't set but true and false within the loop itself.

    Do you see why I'm making this point?

  12. #12
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm afraid that it's not solved. Your boolean, b, will only equal the last evaluation performed. So if you have arrays
    {1, 2, 3} and {3, 3, 3}, since the pair of 3's will be compared last, this will be marked as mirror arrays. Again:

    * set your boolean to true at the beginning.

    * In your loop set it to false if a mismatch is found. Do not set it to true if a match is found. This is what I meant by when I said don't set but true and false within the loop itself.

    Do you see why I'm making this point?
    The computer said I was right, so I'm good :p
    I realized before, the first for loop made each space do the for loop in each one. I don't know how to explain without showing it in person.

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Another option....

    Pretty much the same that the OP has, only a little more cleaner (and with the appropiate correction when the array elements are not the same).

    Java Code:
    import java.lang.*;
    public class MirrorImage
    {
    public static void main(String args[])
    {
    int[] data1 = {1,2,3,4,5};
    int[] data2 = {5,4,3,2,1};
    boolean mirror = false;
    
    for (int i = 0, j = data2.length-1; i<data1.length;i++,j--)
       {
    	if (data1[i]==data2[j])
    	{
    		mirror = true;
    	}
    	else
    	{
    		mirror = false;
    		break;
    	}
       }
     System.out.println("MirrorImage: " + mirror); 
    }
    }
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Basic Question from Networking Beginner
    By JDCAce in forum Networking
    Replies: 7
    Last Post: 10-10-2008, 08:29 PM
  2. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 06:26 AM
  3. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 05:44 PM
  4. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 PM
  5. basic question about files
    By oregon in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 02:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •