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

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-22-2008, 11:59 AM
raj reddy
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
Eranga
I've already answered to this question. Why did you ask it again?

• 04-22-2008, 01:46 PM
sanjeevtarar
And wrong forum too.........
• 04-23-2008, 03:43 AM
Eranga
Yep. I think our friend raj still not read our FAQ Page. :)
• 04-23-2008, 11:01 AM
raj reddy
Facing Problem With For Loops
Quote:

Originally Posted by raj reddy
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
• 04-23-2008, 11:15 AM
Eranga
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
raj reddy
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
• 04-23-2008, 11:47 AM
Eranga
I'm not clear what you say. You want to compare 5 elements at a time?
• 04-23-2008, 12:12 PM
sanjeevtarar
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

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
raj reddy
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
Eranga
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
Eranga
Quote:

Originally Posted by raj reddy
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. :)
• 04-23-2008, 12:30 PM
sanjeevtarar
Quote:

Originally Posted by raj reddy
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

• 04-23-2008, 12:59 PM
Eranga
Quote:

Originally Posted by sanjeevtarar

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.
• 04-23-2008, 01:06 PM
raj reddy
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
Eranga
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
sanjeevtarar
Quote:

Originally Posted by raj reddy
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
• 04-23-2008, 02:09 PM
raj reddy
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 .
• 04-23-2008, 02:38 PM
raj reddy
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
sanjeevtarar

Check with this code..

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--;                         }                 }         } }```
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last