Results 1 to 9 of 9
  1. #1
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    44
    Rep Power
    0

    Default hello all , please help

    rather new to java
    i'm working on these thing and i can't figure it out
    i need a function that gets two arrays and checks if one is in the other or contains the other
    like 11 is in 1314

    123 is in 5231

    also 1123 is in 1132

    and so on
    always getting stuck on that
    sorry if its a newb question
    thanks a lot for the helperssss

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How are you approaching this? post how you have the code designed with code tags.

    my approach would be using two loops and boolean "flag"
    since the first array must all be present in the second array you can set the flag to false if you find an item not present in the second array.

  3. #3
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    44
    Rep Power
    0

    Default

    yea that was my thought but then if let's say we have
    1123 isin 1234 it will come back true or 111 isin 1234 will also be true
    that's the only problem with that the double or triple number

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm sure there are better idea's, but I believe this would work, you could search for an item from the first array in the second array, if it is found, change the value and continue. Since you start by assuming there is a match this would work fine.

    Java Code:
    if(a[i] != 0)
    {
      b[i] = 0;
    }
    else
    {
      b[i] = -1;
    }
    might not work that easily now that I think it through, you would have to search through array1 and make sure none of the items equal 0 to safely set it to 0.
    Last edited by sunde887; 01-13-2011 at 12:48 AM.

  5. #5
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    you can use ArrayList instead to sort them first. then you can easily check whether they are the same.

  6. #6
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    44
    Rep Power
    0

    Default

    ok i did it
    the numbers in the arrays are 0-9
    so i took the first advice and switched it to -1
    works like a charm now

    i didn't try arranging it but yea that might work too

    thanks a lot !!

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by charleyjoyce View Post
    you can use ArrayList instead to sort them first. then you can easily check whether they are the same.
    The containsAll( ... ) method fails for that purpose:

    Java Code:
    import java.util.ArrayList;
    
    
    public class T {
    
    	public static void main(String[] args) {
    
    		ArrayList<Integer> a= new ArrayList<Integer>() {{
    			add(1);
    			add(1);
    		}};
    		
    		ArrayList<Integer> b= new ArrayList<Integer>() {{
    			add(1);
    			add(2);
    			add(3);
    			add(4);
    		}};
    	
    		System.out.println(b.containsAll(a));
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    44
    Rep Power
    0

    Default

    lol a containall function... didn't know that one...
    but will it be ok in the case that you mentioned
    a[]= 1,1
    b[]= 1,2,3,4
    should come back false there

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by smacker View Post
    lol a containall function... didn't know that one...
    but will it be ok in the case that you mentioned
    a[]= 1,1
    b[]= 1,2,3,4
    should come back false there
    Yup, but the ArrayList.containsAll( ... ) method returns true here. Better sort both lists and see if one is a subsequence of the other.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •