Results 21 to 34 of 34
- 04-23-2008, 03:25 PM #21
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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.
- 04-23-2008, 03:51 PM #22
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,संजीव
- 04-23-2008, 04:13 PM #23
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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.
- 04-24-2008, 01:21 PM #24
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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
- 04-25-2008, 03:36 AM #25
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you mean that last comparison not increment the counter. It should be 3, and seems to me your code give the correct value.
- 04-25-2008, 03:37 PM #26
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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 mereddy
- 04-25-2008, 03:48 PM #27
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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 abovereddy
- 04-28-2008, 04:13 AM #28
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 08-10-2009, 11:45 PM #29
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
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);
}
}
- 08-11-2009, 04:28 AM #30
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Not a bad approach. Thanks for sharing.
- 11-20-2009, 12:05 PM #31
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
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.
- 11-21-2009, 04:02 PM #32
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-24-2009, 11:21 AM #33
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Thanks. I have managed to do the code with your help
- 11-25-2009, 05:48 PM #34
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are welcome. If you like just post your code to see others. Anyone can useful your code.
Similar Threads
-
how to compare the elements of these two arraylists
By raj reddy in forum Web FrameworksReplies: 1Last Post: 03-25-2009, 10:55 PM -
A Map implemented with ArrayLists
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:29 PM -
recursively searching through arraylists
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-17-2008, 02:36 AM -
arraylists problem
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-12-2008, 07:38 AM -
Compare 2 XML
By Peter in forum XMLReplies: 1Last Post: 07-05-2007, 02:58 AM


LinkBack URL
About LinkBacks


Bookmarks