Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    raj reddy is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default how to compare the elements of the two arraylists al1,al2

    how to compare the elements of the two arraylists al1,al2


    Let's say we have the following two lists:
    al1=[10, 20, 30, 40]
    al2=[11, 12, 13, 14, 15,16,17,18,19,20]

    we have to compare 10/11,10/12,10/13,10/14,10/15,10/16,10/17,10/18,10/19,10/20,11,11,11/12,11/13,11/14,11/15,......................like that all the elements


    so that we have to compare every element in the first list to every element in the second?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    And wrong forum too.........

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    raj reddy is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default Facing Problem With For Loops

    Quote Originally Posted by raj reddy View Post
    how to compare the elements of the two arraylists al1,al2


    Let's say we have the following two lists:
    al1=[10, 20, 30, 40]
    al2=[11, 12, 13, 14, 15,16,17,18,19,20]

    (i)we have to compare the first 4 elements of al1 to first 5 elements of al2 10/11,10/12,10/13,10/14,10/15,11,11,11/12,11/13,11/14,11/15,10/11,10/12,10/13,10/14,10/15......................like that all the elements

    (ii) and then the same 5 elements of al1 and the next 5 elements of al2

    10/16,10/17,10/18,10/19,10/20,11/16,11/17,11/18,11/19,11/20................................................ ..............like
    that all the elements

    so that we have to compare every element in the first array list, to every five elements at atime in the second arraylist?
    can we get this one by using for loops


    MY CODE

    i have done like this

    for (int i=1;i<arrayList2_Length;i++) {

    for (int j=1;j<arrayList1_Length; j++) {

    if(al2.equals(i)==al1.equals(j))

    }

    }

    CAN YOU MODIFY THIS CODE AND GIVE ME NEW ONE

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In ArrayList call size() to find the number of elements. Make each element as string and compare them. Got it?

  7. #7
    raj reddy is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

    Hi ERANGA





    MY CODE:




    int arrayList1_Length = al1.size();
    int arrayList2_Length = al2.size();


    for (int i=1;i<arrayList2_Length;i++) {

    for (int j=1;j<arrayList1_Length; j++) {



    if(al2.equals(i)==al1.equals(j))

    {




    }





    }



    PROBLEM IS : MY CODE IS COMPARING WITH ALL THE ELEMENTS OF al1 to ALL ELEMENTS OF al2;



    I HAVE DONE LIKE THIS , CAN U MODIFY THIS ONE



    PROBLEM IS : MY CODE IS COMPARING WITH ALL THE ELEMENTS OF al1 to ALL ELEMENTS OF al2;

    JUST I NEED TO COMPARE WITH ALL THE ELEMENTS OF al1 AND ONLY 5 ELEMENTS OF ARRAY 2 AND THEN

    I NEED TO COMPARE WITH ALL THE ELEMENTS OF al1 AND ONLY THE NEXT 5 ELEMENTS OF ARRAY 2 AND

    I NEED TO COMPARE WITH ALL THE ELEMENTS OF al1 AND ONLY THE NEXT 5 ELEMENTS OF ARRAY 2



    WHAT WE HAVE TO DO IS :-

    IT WILL LOOK LIKE



    IN FIRST LOOP


    5/AL1== 5/AL2 WHERE AL2=20 ELEMENTS

    5/AL1== 5/AL2 ie THE NEXT 5 ELEMENTS IN AL2

    5/AL1==5/AL2 ie THE NEXT 5 ELEMENTS IN AL2


    LIKE THAT WE HAVE TO COMPARE
    Last edited by raj reddy; 04-23-2008 at 11:28 AM.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hello Raj,

    Check with code.....
    If i misunderstand your problem please let me know.

    and change the forum ......and if your problem get solved with this then DO MARK THE THREAD SOLVED

    Java Code:
    import java.util.ArrayList;
    public class CompareList
    	{
    
    	public static void main(String[] args){
    		ArrayList list1 = new ArrayList();
    			list1.add(10);
    			list1.add(20);
    			list1.add(30);
    			list1.add(40);
    		ArrayList list2 = new ArrayList();
    			list2.add(11);
    			list2.add(12);
    			list2.add(13);
    			list2.add(14);
    			list2.add(15);
    			list2.add(16);
    			list2.add(17);
    			list2.add(18);
    			list2.add(19);
    			list2.add(20);
    
    		for (int i=0;i<list2.size();i++) {
    			for (int j=0;j<list1.size(); j++) {
    				if(list2.get(i).equals(list1.get(j)))
    					System.out.println("equals..:"+list1.get(j));
    				}
    			}
    	}
    }



    sanejev

  10. #10
    raj reddy is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

    Exactly i need to compare only five elements in al2 at a time with all the elements of al1


    note: in al2 there are 20 elements

    in al1 there are only 5 elements

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Sanjeev, I'm confusing on that he is talking about 5 element searching at a time. Did you get the point?

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by raj reddy View Post
    Exactly i need to compare only five elements in al2 at a time with all the elements of al1


    note: in al2 there are 20 elements

    in al1 there are only 5 elements

    Sanjeev give a code for you. Think about the loops there. Sanjeev do the iteration for the complete size of the ArrayList. You need to do it for five elements. :)

  13. #13
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by raj reddy View Post
    Exactly i need to compare only five elements in al2 at a time with all the elements of al1


    note: in al2 there are 20 elements

    in al1 there are only 5 elements

    I think eranga he wants to compare all elements of list1 with FIRST FIVE elements of list2.

    Ok and then what about remaining elements of list2..??

    raj do you need to compare them again or there is another logic ....this is confusing me too.


    sanjeev


  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sanjeevtarar View Post



    Ok and then what about remaining elements of list2..??

    raj do you need to compare them again or there is another logic ....this is confusing me too.

    Yes it is. May be he want to search only five elements of it.

  15. #15
    raj reddy is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

    yes we have to compare the next 5 elements also sanjeev

    it will continue upto the last 5 elements in the array

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    So why don't you search all the elements at once. Do you have to make any other execution after each 5 elements searching.

  17. #17
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by raj reddy View Post
    yes we have to compare the next 5 elements also sanjeev

    it will continue upto the last 5 elements in the array
    Ok .... at last you are comparing all elements of list1 with list2 ...am i right.

    then why are you comparing like that ......??

    what i understand is that : when you find an element of list1 in to list2's top 5 you will do something or exit the loop...if you do not find in first 5 then you will search next 5.....am i right



    sanjeev

  18. #18
    raj reddy is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

    hi sanjeev



    i will give u a clear idea




    think in


    array al1={1,2}

    array al2={1,2,3,4,5,6,7,8,9,10,11,12}


    Now what i have to do is


    if (al1(elements)==compare al2(only4 elements 1,2,3,4))
    {
    /*continues my logic*/
    }
    else if (al1(elements)==compare al2(only4 elements)ie 5,6,7,8)
    {
    /*continues my logic*/
    }


    else if (al1(elements)==compare al2(only4 elements)ie 8,9,10,11,12)
    {
    /*continues my logic*/
    }


    the above output of comparing the two arrays i need in two for loops then the CODE will be


    MY CODE:



    array al1={1,2}

    array al2={1,2,3,4,5,6,7,8,9,10,11,12}



    int arrayList1_Length = al1.size();
    int arrayList2_Length = al2.size();





    for (int i=1;i<arrayList2_Length;i++) {




    for (int j=1;j<arrayList1_Length; j++) {

    {




    }}
    }




    In my code all the values of all are comparing with all the elements of al2.

    This is not what i need.


    I need to be compare everytime with all the elements of al1 and only 4 elements of array2 (upto the end of all the elements of array al2 )



    that means al1 will be constant


    al2 will be looping for every 4 elements


    think that there were 12 elements in al2, so 4 times the inner loop will move



    i need the code in the loops


    NOTE: DONOT CONSIDER WHETHER THE ELEMENTS WERE MATCHABLE OR NOT, ONLY WE HAVE TO COMPARE THE VALUES ACCORDING TO THE NO OF ELEMENTS , I WILL WRITE MY OWN LOGIC ISIDE THE LOOPS

    ANY QUERIES REPLY ME BACK .
    Last edited by raj reddy; 04-23-2008 at 02:13 PM.

  19. #19
    raj reddy is offline Member
    Join Date
    Apr 2008
    Posts
    31
    Rep Power
    0

    Default

    i will tell u in a simple way

    if we do not find or if we found in first 5 , then also we have to search

    in the next 5.....upto last 5 elements in that array


    donot consider matchables or not


    we have to loop basing upon the above condition

  20. #20
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default


    Check with this code..

    Java Code:
    import java.util.ArrayList;
    public class CompareList
    	{
    
    	public static void main(String[] args){
    		ArrayList list1 = new ArrayList();
    			list1.add(10);
    			list1.add(20);
    			list1.add(30);
    			list1.add(40);
    		ArrayList list2 = new ArrayList();
    			list2.add(11);
    			list2.add(12);
    			list2.add(13);
    			list2.add(14);
    			list2.add(15);
    			list2.add(16);
    			list2.add(17);
    			list2.add(18);
    			list2.add(19);
    			list2.add(20);
    		
    		int s = 1;
    		for (int i=0;i<list2.size();i++) {
    			if(s <= 4){
    				System.out.println("here..:"+list2.get(i));
    				for (int j=0;j<list1.size(); j++) {
    					if(list2.get(i).equals(list1.get(j)))
    						System.out.println("equals..:"+list1.get(j));
    				}
    				s++;
    			}else{
    				System.out.println("Checked Four elements, Now checking next Four");
    				s = 1;
    				i--;
    			}
    		}
    
    	}
    }
    sanjeev,संजीव

Page 1 of 2 12 LastLast

Similar Threads

  1. how to compare the elements of these two arraylists
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 03-25-2009, 10:55 PM
  2. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:29 PM
  3. recursively searching through arraylists
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:36 AM
  4. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 AM
  5. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 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
  •