# help with recursive return boolean

Printable View

• 01-10-2011, 03:17 PM
Yakg
help with recursive return boolean
Hi, I'm trying to write a program that compares two arrays to check if they have the same values (unsorted).
This is the condition I'd like to use just can't get to the true value.

here's the code:
-----------------------------------------------------------------------
Code:

```public static boolean mymeth (int a[],int b[]){ int i=1,j=1,found=0; return mymeth (a,b,i,j); }```
Code:

```private static boolean mymeth (int [] a,int [] b,int i,int j){ if (j<a.length-1 && i<b.length && a[i] != b [j]) return mymeth (a, b,i,j+1); else if (a[i-1] == b [j]) return mymeth (a, b,i+1,j=0); else if (i==a.length) return true; else return false;     }     }```
----------------------------------------------------------------------
//thank you.
• 01-10-2011, 05:08 PM
doWhile
And what is the problem/question? Are there exceptions? Is it functioning correctly?
• 01-10-2011, 05:27 PM
Yakg
it suppose to..
compare between two arrays to check if all their values are equals (unsorted)
so for ex. if a [] = {1,2,3,4} and b [] = {4,2,1,3} need to return true

In my case always returned false..

I'm new to recursive and hope you could help me understand it.
Thank you.
• 01-10-2011, 05:51 PM
AcousticBruce
I am not sure if you have to use arrays and if you have a particular format you need to follow, so I hope this helps.
I have been messing with Iterators with collections and I know this way does what you are asking.

Personally I haven't tried Iterator with Arrays but they definitely work with ArrayLists.

Code:

``` import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class CollectionTest {     String[] things1 = {"car", "truck", "plane", "bike", "moped"};     List<String> list1 = new ArrayList<String>();     String[] things2 = {"moped", "plane", "car", "bike", "truck"};     List<String> list2 = new ArrayList<String>();     public void cTest() {         for(String x: things1) {             list1.add(x);         }         for(String x: things2) {             list2.add(x);         }         checkIfEqual(list1, list2);     }     private void checkIfEqual(List<String> mainList, List<String> checkList) {         Iterator<String> it = checkList.iterator();         boolean isTheSame = true;         int howMany = 0;         while(it.hasNext()) {             if (!mainList.contains(it.next())) {                 isTheSame = false;                 howMany++;             }         }         if (isTheSame) {             System.out.println("The lists contain the same data");         } else {             System.out.printf("\nThere are %d different items in list 2", howMany);         }     } }```

I just altered the code in mine just a bit to see if I can do what you were wanting. Im glad you asked the question, because I learned something.

I have not had a successful program that does this without lists. So now I have a new project.
There could be a more efficient way of doing this. So I guess we will see when and if the pros respond :)
• 01-10-2011, 06:21 PM
hosscomp
Quote:

Hi, I'm trying to write a program that compares two arrays to check if they have the same values (unsorted).
This is the condition I'd like to use just can't get to the true value.
Are you required to use recursion? If not, I would sort both arrays (if they are the same size) then compare each element.
• 01-10-2011, 07:19 PM
Yakg
yes, we are not allowed to sort them or use
loops, just recursion..

I was thinking maybe with overloading and to use a void method to help..
any ideas?

Really appreciating the great effort.
• 01-10-2011, 10:01 PM
hosscomp
Quote:

yes, we are not allowed to sort them or use
loops, just recursion..

I was thinking maybe with overloading and to use a void method to help..
any ideas?

Really appreciating the great effort.
Add to Yakg's Reputation
That is a strange problem for recursion.
I came up with the following:
Code:

```  private static boolean mymeth(ArrayList<Integer> a, ArrayList<Integer> b) {     if (a.size() != b.size()) {       // can't be equal       return false;     }     // System.out.println(a);     // System.out.println(b);     // System.out.println();     if (b.contains(a.get(0))) {       b.remove(b.indexOf(a.get(0)));       a.remove(0);       if (a.size() == 0) {         // all match         return true;       }       else {         return mymeth(a, b);       }     }     else {       // element not found in b.       return false;     }   }```
This will mutate the ArrayLists passed in, so keep a copy if you need them later.

Code:

```  public static void main(String[] args) {     Integer[] a = {1, 2, 3, 4};     Integer[] b = {4, 2, 1, 3};     ArrayList aList = new ArrayList(Arrays.asList(a));     ArrayList bList = new ArrayList(Arrays.asList(b));     System.out.println("compare = " + mymeth(aList, bList));   }```
• 01-10-2011, 10:05 PM
AcousticBruce
I noticed you said it cant sort, so do you have to use recursion?
can you use ArrayList? Can you use Iterator?

Im interested in seeing the assignment (im assuming this is an assignment) explanation. Do you mind?
• 01-10-2011, 10:10 PM
AcousticBruce
If you change a couple things in my code from above, you can have it return a boolean value.

Code:

``` public [COLOR="Red"]boolean[/COLOR] checkList(List<String> mainList, List<String> checkList) {         Iterator<String> it = checkList.iterator();         boolean isTheSame = true;         while(it.hasNext()) {             if (!mainkList.contains(it.next())) {                 isTheSame = false;             }         }         [COLOR="Red"]if (isTheSame) {             return true;         } else {             return false;         }[/COLOR]     }```

then just do...
Code:

`System.out.println(checkList(list1, list2));`
• 01-11-2011, 04:13 AM
gcalvin
Recursive methods should follow this basic pattern:

1) Check a trivial "base case" -- if conditions match the base case, I'm all done and can return a simple result.
2) If conditions don't match the base case, I want to do one simple thing that makes the larger case just a little bit simpler, then...
3) hand off the rest of my conditions to another invocation of myself.

In this case, you are starting with two unsorted int arrays. You want your method to return true if all of the elements in the first array exist once and only once in the second array.

The base case is that both arrays are empty. You can return true, and you're done.

If the arrays are not empty, it might make sense to check if the arrays are the same size. If they're not, you can return false and you're done.

Next, you want to take the first element of the first array, and see if it's present in the second array. If it's not there, you can return false and you're done. If it is there, you need to call another iteration of yourself, passing copies of your two arrays with the element you just compared removed, and return its result. One of these iterations will either compare two empty arrays and return true, or find a mismatch and return false.

-Gary-
• 01-11-2011, 04:24 PM
Yakg
Solved!!!
it's good to know there's a place I can turn to in need.
Solved it with the change of the base rule, had to made one that doesn't exist.

Thank you for your amazing help and dedication :).
• 01-11-2011, 06:54 PM
hosscomp
Can we see your solution?