# Thread: help with recursive return boolean

1. Member
Join Date
Dec 2010
Posts
63
Rep Power
0

## 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:
-----------------------------------------------------------------------
Java Code:
```public static boolean mymeth (int a[],int b[]){
int i=1,j=1,found=0;
return mymeth (a,b,i,j);
}```
Java 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.

2. Moderator
Join Date
Jul 2010
Location
California
Posts
1,641
Rep Power
9
And what is the problem/question? Are there exceptions? Is it functioning correctly?

3. Member
Join Date
Dec 2010
Posts
63
Rep Power
0

## 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.

4. Senior Member
Join Date
Dec 2010
Location
Indiana
Posts
202
Rep Power
7
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.

Java 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) {
}

for(String x: things2) {
}
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 :)
Last edited by AcousticBruce; 01-10-2011 at 10:29 PM.

5. Member
Join Date
Oct 2010
Posts
63
Rep Power
0
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.

6. Member
Join Date
Dec 2010
Posts
63
Rep Power
0

## 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.

7. Member
Join Date
Oct 2010
Posts
63
Rep Power
0
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.
That is a strange problem for recursion.
I came up with the following:
Java 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 {
return false;
}
}```
This will mutate the ArrayLists passed in, so keep a copy if you need them later.

Java 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));
}```

8. Senior Member
Join Date
Dec 2010
Location
Indiana
Posts
202
Rep Power
7
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?
Last edited by AcousticBruce; 01-10-2011 at 10:12 PM.

9. Senior Member
Join Date
Dec 2010
Location
Indiana
Posts
202
Rep Power
7
If you change a couple things in my code from above, you can have it return a boolean value.

Java 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...
Java Code:
`System.out.println(checkList(list1, list2));`
Last edited by AcousticBruce; 01-10-2011 at 10:29 PM.

10. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
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-

11. Member
Join Date
Dec 2010
Posts
63
Rep Power
0

## 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 :).

12. Member
Join Date
Oct 2010
Posts
63
Rep Power
0