Results 1 to 13 of 13
Thread: Basic array question
- 01-08-2009, 04:59 AM #1
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Basic array question
This was the question but I am totally lost
They also gave me thisCreate 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
I put this in, but it's messy and theres something wrong...
public boolean mirrorImage (int[] data1, int[] data2) {
I get this error, it's telling me I'm pointing a place outside of the array or something
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;Any help please :Druntime error (line 8, column 0):
java.lang.ArrayIndexOutOfBoundsException: 10
-> array.ArrayQuestions.mirrorImage()
-
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:
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.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(...) //... } }
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.
- 01-08-2009, 05:20 AM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
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
-
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!
- 01-08-2009, 05:49 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
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...
-
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.
- 01-08-2009, 06:24 AM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
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. :(
-
- 01-08-2009, 09:16 PM #9
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
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;
- 01-08-2009, 09:17 PM #10
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Oh and how do I mark my thread as solved?
-
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?
- 01-09-2009, 12:45 AM #12
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
- 01-09-2009, 04:44 PM #13
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
-
Basic Question from Networking Beginner
By JDCAce in forum NetworkingReplies: 7Last Post: 10-10-2008, 08:29 PM -
Newbie search array question
By CirKuT in forum New To JavaReplies: 19Last Post: 09-14-2008, 06:26 AM -
Basic question about EJB
By javaplus in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-15-2008, 05:44 PM -
Very basic question
By gvi in forum New To JavaReplies: 2Last Post: 10-30-2007, 06:30 PM -
basic question about files
By oregon in forum New To JavaReplies: 1Last Post: 08-05-2007, 02:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks