Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: matching elements of one array with another array.

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default matching elements of one array with another array.

    I have 3 arrays.
    array1 has a b(suppose)
    array2 has a
    array 3 has b
    how to find in which array ,array1 elements are present?
    here all 3 array may or may not be of same length.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: matching elements of one array with another array.

    how to find in which array ,array1 elements are present?
    I think you have to code some loops and compare the elements of the arrays.
    What have you tried?l
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: matching elements of one array with another array.

    ya i have tried with loops.since arrays may be of different length am getting ArrayIndexOutOfBoundException.So am not able to code.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: matching elements of one array with another array.

    The code needs to test that the index it is using does not go past the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: matching elements of one array with another array.

    Based on post #3 it sounds like you are comparing arrays index for index. Are you looking for exact matches in the same spot or just if the item is in the other array anywhere? If that is the case then it sounds like you are looking for some nested for loops. As long as you put the length of the array as part of the condition for looping you should never get an ArrayIndexOutOfBoundException.

    Do you have code you can show or a better description of the problem you are trying to solve?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  6. #6
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: matching elements of one array with another array.

    Ya i want to know items in one array, say array1 is present in which array.Not the order.I just want to know which array contains elements of array1.Am sorry am not able to code properly for this.So can't show you my code.It's incorrect.

  7. #7
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: matching elements of one array with another array.

    That's what we're here for. Show us the incorrect code and we will help you work through it. Trust me, we see some god aweful code here and you wont be ridiculed.

    Anyways, sounds like you just need some nested for loops. Try listing what needs to happen in basic english. This always helps me notice patterns that can be applied as loops or conditional branches.

    Something like: FOR EACH item in this array, look at EACH item in this other array and determine IF they are equal. Hey, look at those key words. Looks like i can apply that directly to my code.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  8. #8
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: matching elements of one array with another array.

    public class arrays
    {
    public static void main(String[] args)
    {
    String int_array[]=new String[20];//int_array has a b c elements
    String float_array[]=new String[20];//float_array has a1 b1  elements
    String array[]=new String[20];//array has a b b1 elements
    StringBuffer  sb = new StringBuffer(); 
                                         for(int i=0;i<array.length;i++)
    				{
    		                      for(int j=0;j<int_array.length;j++)
    		                {      
    		        	        if(array[i].contains(int_array[j]))
    		        	      {
    		                         sb.append("%d");
    		                         break;
    		        	   }
    		           }
    		                 for(int k=0;k<float_array.length;k++)
    		        	   {
    		        	        if(array[i].contains(float_array[k]))
    		        	      {
    		        		   sb.append("%f");
    		        		   break;
    		        	   }
    		        	 }
    		               }	
    }
    }
    here 'array' has some items.i want to check array[i]th item is in int_array or float_array.

  9. #9
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: matching elements of one array with another array.

    The basic format of the program is what you're looking for. Make sure your if compare is working as you would expect it. I dont think .contains is going to give you your desired result.

    PS: My old Comp Sci teacher would crucify you for putting a break in a for loop although i would argue it is a legitimate strategy.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  10. #10
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: matching elements of one array with another array.

    ya .contains doesn't work out,instead i used .matches.It's working fine.And i removed break statement.
    i just want to know if my approach in writing code was rite or i need some changes in the code i posted.

  11. #11
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: matching elements of one array with another array.

    The approach works. There are other methods for finding elements in arrays, but that's a lot of computer science theory i dont care to get into.

    Continuing in a for loop once an element is found is asinine in my opinion. If i'm looking for butter in the fridge and i find it, i dont continue looking just because there's more stuff in the fridge. One way to break out of a for loop without using an explicit "break" is to add to the conditional check at the top. Almost making it a for loop/do while hybrid. See my example.
    boolean found = false;
    for(int i=0 ; i < array.length && found==false ; i++){
        if(array[i] == itemToFind){
            found=true;
        }
    }
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  12. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    shenaz (March 29th, 2013)

  13. #12
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: matching elements of one array with another array.

    ya rite.I will include conditional check in for loop.Thanks alot for responding in early

  14. #13
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: matching elements of one array with another array.

    Any other questions? If not please mark thread as Solved.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  15. #14
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: matching elements of one array with another array.

    No more questions regarding this.Thank you.And i have already marked it as solved.

Similar Threads

  1. If Statements Using Elements in an Array
    By grayaa93 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2013, 08:35 AM
  2. How to sum up elements in an array?
    By D-X69 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2012, 01:21 AM
  3. [SOLVED] How To Increment Array Elements
    By Nuggets in forum Java Theory & Questions
    Replies: 15
    Last Post: April 1st, 2012, 12:10 PM
  4. Missing elements in array
    By frozen java in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 10th, 2012, 10:52 PM
  5. Array elements comparison
    By Pradeep_Parihar in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2011, 08:45 AM