Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing arraylist elements  RSS feed

 
joseph cooper
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an arraylist, clientList, which holds an array of clientRecords which has a string value of a sequence number. I also have an arraylist, policyList, which holds an array of policyRecords which has a string value of a sequence number. I am trying to figure out how to compare the two sequence numbers against both arraylist?

ArrayList clientList
Object[0] clientRecord
String sequenceNumber = "1"
String companyText = "AFG"
Object[1] clientRecord
String sequenceNumber = "2"
String companyText = "FFD"
Object[2] clientRecord
String sequenceNumber = "3"
.
.
.

ArrayList policyList
Object[0] policyRecord
String sequenceNumber = "2"
String policyNumber = "F12"
Object[1] policyRecord
String sequenceNumber = "2"
String policyNumber = "WW1"
Object[2] policyRecord
String sequenceNumber = "1"
.
.
.



I can not figure out how to get down to the level of the elements of the records?

Any help would be appreciated.
Thanks in advance.
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to figure out how to compare the two sequence numbers against both arraylist?


Could you explain what you mean by this? Are you trying to extract elements from each collection with the corresponding sequence numbers?
 
joseph cooper
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compare the element sequenceNumber from the client record of the arraylist clientList to each of the elements sequenceNumber from the policy record of the arraylist policyList.

This is not correct code.
//cr is the client record
//pr is the policy record

for(int index=0; index<clientList.size(); index++)
{
for(int i=0; i<policyList.size(); i++)
{
if (clientList[index].get(Integer.parseInt(cr.sequenceNumber)).equals(policyList[i].get(Integer.parseInt(pr.sequenceNumber))))
{
/* do some work if they match*/
} //if
} //for - policy
} //for - client
 
Tempora Telora
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by joseph cooper:
I am trying to compare the element sequenceNumber from the client record of the arraylist clientList to each of the elements sequenceNumber from the policy record of the arraylist policyList.

This is not correct code.
//cr is the client record
//pr is the policy record

for(int index=0; index<clientList.size(); index++)
{
for(int i=0; i<policyList.size(); i++)
{
if (clientList[index].get(Integer.parseInt(cr.sequenceNumber)).equals(policyList[i].get(Integer.parseInt(pr.sequenceNumber))))
{
/* do some work if they match*/
} //if
} //for - policy
} //for - client



Your pseudo seems correct. You are using a bubble sort to compare each client against the policy. If policy and client sequence are equal then you need to retrieve then use your .get methods ( client[1].getFirstName(), etc..) that you have created to compare and retreive your data items. You can store the data from your .get methods from both the client and policy into variables and use those for transformation purposes.

Other than that I am completely lost as to what you need as well. It seems as if you have it already figured out in your pseudo.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you are trying to find keys that are in both collections. Using a Map would make this easier. Here is some pseudo code





or assuming you make a copy of the collections which allows you to alter it then you could use a collection method like retainAll that allows you to remove any elements that aren't in both.



http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html#retainAll(java.util.Collection)
[ November 13, 2007: Message edited by: steve souza ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well in general, what you are grappling with is called a nested loop. You would do well to implement your work as a two-level loop nesting, whcih can be done with an iterator - google for public interface Iterator, such as that discussed at An iterator over a collection.,.... then struggle with it until you can write


You will have to get two iterators to do this, and you should post questions about how to use iterators somewhat separately from this thread ( unless the moderator says otherwise )

Very, very carefully figure out which is the inner loop and which is the outer loop and which whatchamacallit goes where, just put what you think is supposed to happen in the innermost loop and work backwards from there to the outermost containment loop. Note that the iterator for the outer loop is obtained right before entry into the outer loop, same for inner loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!