Results 1 to 4 of 4
  1. #1
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default searching one elements of array into another array???

    Hello I have two char array
    first one contains 'A','B','D','E'
    second one contains 'C','E','A'

    How can I find second array's elements which does not exist in first array?
    result should be 'C'

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: searching one elements of array into another array???

    The collection to use here is a Set. Put the first collection into a set, removeAll() the elements of the second collection and the difference you are left with is what you are trying to calculate.

    The Set Interface in Oracle's Tutorial describes the usage of this collection - including a description of the very problem you pose in the section "Set Interface Bulk Operations".

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

    Default Re: searching one elements of array into another array???

    Quote Originally Posted by hacikho View Post
    Hello I have two char array
    first one contains 'A','B','D','E'
    second one contains 'C','E','A'

    How can I find second array's elements which does not exist in first array?
    result should be 'C'
    Just because I'm feeling a bit obnoxious tonight:

    Java Code:
    public class T {
    	public static void main(String[] args) {
    
    		char[] first= { 'A','B','D','E' };
    		char[] second = { 'C','E','A' };
     
    		String s1= new String(first);
    		String s2= new String(second);
    		
    		System.out.println(s2.replaceAll("["+s1+"]", ""));
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default Re: searching one elements of array into another array???

    I appreciate JosAh and pbrockway2 , thanks for helping.

Similar Threads

  1. Searching array elements for keywords
    By Nanomech in forum New To Java
    Replies: 2
    Last Post: 08-13-2011, 01:46 PM
  2. Searching through Array of Objects
    By coopc in forum New To Java
    Replies: 6
    Last Post: 04-26-2011, 03:32 PM
  3. Replies: 3
    Last Post: 03-15-2011, 12:24 PM
  4. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  5. Searching and comparing Array elements
    By jmanswrd in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 06:06 AM

Posting Permissions

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