Results 1 to 12 of 12
- 01-10-2011, 02:17 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 59
- 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.
- 01-10-2011, 04:08 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
And what is the problem/question? Are there exceptions? Is it functioning correctly?
- 01-10-2011, 04:27 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 59
- 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.
- 01-10-2011, 04:51 PM #4
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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) { 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 :)Last edited by AcousticBruce; 01-10-2011 at 09:29 PM.
- 01-10-2011, 05:21 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
Are you required to use recursion? If not, I would sort both arrays (if they are the same size) then compare each element.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.
- 01-10-2011, 06:19 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 59
- 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.
- 01-10-2011, 09:01 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
That is a strange problem for recursion.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
I came up with the following:
This will mutate the ArrayLists passed in, so keep a copy if you need them later.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 { // element not found in b. return false; } }
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)); }
- 01-10-2011, 09:05 PM #8
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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 09:12 PM.
- 01-10-2011, 09:10 PM #9
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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 09:29 PM.
- 01-11-2011, 03:13 AM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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, 03:24 PM #11
Member
- Join Date
- Dec 2010
- Posts
- 59
- 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 :).
- 01-11-2011, 05:54 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
use boolean as 0 or 1
By joost_m in forum New To JavaReplies: 10Last Post: 04-13-2010, 11:22 AM -
Return Statement and Boolean Help
By GhostShaman in forum New To JavaReplies: 8Last Post: 03-09-2010, 11:15 AM -
Boolean Return Value
By devstarter in forum New To JavaReplies: 5Last Post: 03-02-2010, 07:45 AM -
Recursive Method
By bluegreen7hi in forum New To JavaReplies: 5Last Post: 11-29-2007, 04:45 AM -
Recursive Anagram
By zoe in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks