Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-22-2008, 12:59 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-22-2008, 01:38 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I've already answered to this question. Why did you ask it again?

__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-22-2008, 02:46 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
And wrong forum too.........
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-23-2008, 04:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yep. I think our friend raj still not read our FAQ Page.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-23-2008, 12:01 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-23-2008, 12:15 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
In ArrayList call size() to find the number of elements. Make each element as string and compare them. Got it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-23-2008, 12:20 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
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 12:28 PM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-23-2008, 12:47 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I'm not clear what you say. You want to compare 5 elements at a time?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-23-2008, 01:12 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-23-2008, 01:19 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-23-2008, 01:19 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Sanjeev, I'm confusing on that he is talking about 5 element searching at a time. Did you get the point?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-23-2008, 01:25 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-23-2008, 01:30 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
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

Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-23-2008, 01:59 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-23-2008, 02:06 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
yes we have to compare the next 5 elements also sanjeev

it will continue upto the last 5 elements in the array
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-23-2008, 02:08 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to 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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 04-23-2008, 02:11 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 04-23-2008, 03:09 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
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 03:13 PM.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 04-23-2008, 03:38 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
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
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 04-23-2008, 03:52 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road

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--; } } } }
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes