Results 1 to 20 of 34
- 04-22-2008, 11:59 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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?
- 04-22-2008, 12:38 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I've already answered to this question. Why did you ask it again?
- 04-22-2008, 01:46 PM #3
And wrong forum too.........
- 04-23-2008, 03:43 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-23-2008, 11:01 AM #5
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
- 04-23-2008, 11:15 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In ArrayList call size() to find the number of elements. Make each element as string and compare them. Got it?
- 04-23-2008, 11:20 AM #7
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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 COMPARELast edited by raj reddy; 04-23-2008 at 11:28 AM.
- 04-23-2008, 11:47 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I'm not clear what you say. You want to compare 5 elements at a time?
- 04-23-2008, 12:12 PM #9
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
- 04-23-2008, 12:19 PM #10
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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
- 04-23-2008, 12:19 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Sanjeev, I'm confusing on that he is talking about 5 element searching at a time. Did you get the point?
- 04-23-2008, 12:25 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-23-2008, 12:30 PM #13
- 04-23-2008, 12:59 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-23-2008, 01:06 PM #15
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
yes we have to compare the next 5 elements also sanjeev
it will continue upto the last 5 elements in the array
- 04-23-2008, 01:08 PM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So why don't you search all the elements at once. Do you have to make any other execution after each 5 elements searching.
- 04-23-2008, 01:11 PM #17
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
- 04-23-2008, 02:09 PM #18
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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.
- 04-23-2008, 02:38 PM #19
Member
- Join Date
- Apr 2008
- Posts
- 31
- Rep Power
- 0
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
- 04-23-2008, 02:52 PM #20
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,संजीव
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