Java method to check two int arrays to see if they are mirror images?
-
Can't seem to figure this out. Question is: 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. public boolean mirrorImage (int [ ] data1, int [ ] data2) { //CODE }
-
Answer:
Not sure if this is the most effective way but, give it a try public boolean mirrorImage (int [ ] data1, int [ ] data2) { boolean answer=true; //--------------------------------------… /*In case the lenght of each array is different, it will take the smallest array as the limit for the next step so theres no error while incrementing*/ int x=data1.length; int z=data2.length; int limit=0; if(x<z){ limit=x; }else{ limit=z;// z is equal or less than x } /*if u know both arrays are the same just set, int limit=data1.length; so u dont have to wrttie all this*/ //--------------------------------------… for(int index=0;index<limit;index++){ //if any of the data its not the same it will return a false if(data1[index]!=data2[index]){ answer=false; } } return answer; }
Kevin at Yahoo! Answers Visit the source
Other answers
Do something like: if (data1.length != data2.length) return false; for (int i = 0, j = data1.length; i == data1.length; i++, j--) if (data1[i] != data2[j]) return false; return true; You might need to revise it slightly to be accurate Java code.
Related Q & A:
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- How to implicitly check method arguments in Python?Best solution by stackoverflow.com
- How to merge two arrays of objects in angular?Best solution by Stack Overflow
- How to Get Main output into Common() method in Java?Best solution by pages.cs.wisc.edu
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.