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

    Default

    hi Sanjeev


    The code is exactly working fine.

    BUT I NEED TO GET THE PRIZE LEVELS DEPENDING UPON THE NUMBER OF COUNTS;


    I HAVE GIVEN THE count++ in the for loop.


    but am not getting the updated count value ;



    can u please check and resubmit the code whether the count++ i have given in the loop is at correct location or not.





    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;

    int count=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)))

    count++;

    System.out.println("equals..:"+list1.get(j));
    }
    s++;
    }else{
    System.out.println("Checked Four elements, Now checking next Four");
    s = 1;
    i--;
    }
    }


    if(count==al2.size()-4) {
    System.out.println("The level of the prize is FOUR "+count);
    }else if(count==al2.size()-3) {
    System.out.println("The level of the prize is THREE"+count);
    } else if(count==al2.size()-2) {
    System.out.println("The level of the prize is TWO "+count);
    } else if(count==al2.size()-1) {
    System.out.println("The level of the prize is ONE "+count);
    } else if(count==al2.size()-0) {
    System.out.println("The level of the prize is ZERO "+count);
    }
    else System.out.println("There is no prize level ");

    }
    }
    Last edited by raj reddy; 04-23-2008 at 03:28 PM.

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

    Default





    I did not understand your count logic.
    You will get count value : Number of matches (start count with 0).
    count is giving correct value

    check 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;
    
    	int count=0;
    
    		for (int i=0;i<list2.size();i++) {
    			if(s <= 4){
    				System.out.println("Values..:"+list2.get(i));
    				for (int j=0;j<list1.size(); j++) {
    						if(list2.get(i).equals(list1.get(j))){
    							count++;
    							System.out.println("equals..:"+list1.get(j));
    						}
    				}
    				s++;
    			}else{
    				System.out.println("Checked Four elements, Now checking next Four");
    				s = 1;
    				i--;
    			}
    		}
    
    		System.out.println(count);
    		if(count==list2.size()-4) {
    		System.out.println("The level of the prize is FOUR "+count);
    		}else if(count==list2.size()-3) {
    		System.out.println("The level of the prize is THREE"+count);
    		} else if(count==list2.size()-2) {
    		System.out.println("The level of the prize is TWO "+count);
    		} else if(count==list2.size()-1) {
    		System.out.println("The level of the prize is ONE "+count);
    		} else if(count==list2.size()-0) {
    		System.out.println("The level of the prize is ZERO "+count);
    		}
    		else System.out.println("There is no prize level ");
    
    	}
    }

    What exactly mean...of .... if(count==list2.size()-4)




    sanjeev,संजीव

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

    Default

    if(count==list2.size()-3) means






    the count value will be added if one value or more values are matchable for every loop



    this count should be added by using count++



    ie for every 4 values (which exist in list1) and for every 4 values (which exist in list2)



    suppose 2 values were there means count will be 2 for that loop




    if(count==list2.size()-3) means




    in the above one list2.size()-3 is





    the actual value of list2.size() is 4


    if we subtract 1 value from that one is 3



    so


    if 3(this is count value)=3 (this is list2.size()-3) value

    {

    System.out.println("The level of the prize is THREE"+count);
    }



    SO IN THE ABOVE CODE IN WHICH FOR LOOP I HAVE TO PLACE THE count++
    Last edited by raj reddy; 04-23-2008 at 04:16 PM.

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

    Default

    Hi sanjeev








    Requirement : count variable should increment upto the last comparison of the arrays.

    ie count=1,2,3,4,5....upto last comparison of the arrays like that


    and also count variable should be increased if and only if the two arrays values are equal

    everything is fine but only the count variable is not incrementing for the last comparison of the arrays

    Let us think that there are two arrays al1 and al2;




    MY CODE:

    al1={1,2,3}


    al2=(1,2,3,4,5,6,7,8,9}



    int count=0;


    for (int i=0;i<al1.size();i++) {
    {

    for (int j=0;j<al2.size(); j++) {



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

    count++;


    }


    Actually the count variable should increment for every comparison of the al1 and al2.


    MY PROBLEM IS : THe variable is incrementing fine for every comparison , but for the last comparison of the arrays the count variable is not incrementing.


    can u validate the count variable such a way that i have to get the last comparison value of the two arrays.

    PLS SEND ME THE UPDATED CODE

  5. #25
    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

    What you mean that last comparison not increment the counter. It should be 3, and seems to me your code give the correct value.

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

    Default

    hi eranga

    when the two arrays were comparing


    ie if(al1.get(i).equals(al2.get(j)))


    the count variable should be incremented


    but for the last comparison of the values in the arrays

    the count variable is not incrementing

    if u have any queries reply me
    reddy

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

    Default

    hi Eranga


    the count variable should be incremented for every element comparison of the arrays

    for eg

    int count=0;

    al1.(2)=al2.(2)
    count++;

    the above arrays were equal, so the count variable will be incremented to 1;

    then go for next comparison

    al1.(3)=al2.(3)
    count++;

    the count variable will be incremented to 2;


    like that the count variable will be go on incrementing if the number of equal comparisons were there


    what my problem is :


    the count variable is not incrementing for the last comparison of the arrays


    can u check in the code i have sent above
    reddy

  8. #28
    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

    Your code give the correct value.

    May be you refer the count variable in wrong place. According to your array lists the count variable hold the 3 as final result. It can't be 2 as you said.

  9. #29
    sreeg is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default

    I just found this thread. I case any one is interested:

    import java.util.ArrayList;
    public class CompareList
    {

    @SuppressWarnings("unchecked")
    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);
    ArrayList list3 = new ArrayList();
    Object sree = null;

    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));
    sree = list1.get(j);
    }
    if(list2.get(i).equals(list1.get(j)))
    {
    list3.add(sree);
    }
    }


    }
    System.out.println("Final list3:" + list3);
    }
    }

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

  11. #31
    shalini_priya is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    How to find out the starting position of the second array from the first array without using the API methods?


    Let's say we have the following two lists:
    int [] a1=[2, 7, 8, 4, 6, 0, 3, 9, 1, 5]
    int [] a2=[6, 0, 3]

    Note: The 1st array is an unsorted array. It should not be sorted.

    Please help me out asap.

  12. #32
    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 shalini_priya View Post
    How to find out the starting position of the second array from the first array without using the API methods?


    Let's say we have the following two lists:
    int [] a1=[2, 7, 8, 4, 6, 0, 3, 9, 1, 5]
    int [] a2=[6, 0, 3]

    Note: The 1st array is an unsorted array. It should not be sorted.

    Please help me out asap.
    Then you've to define your own logic/algorithm for that. Just as a hint, search through the two arrays and keep tack on the same order in both arrays. Once the same element found in both arrays keep track on the next element, either same or not.

  13. #33
    shalini_priya is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks. I have managed to do the code with your help

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

Page 2 of 2 FirstFirst 12

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
  •