| Author |
Java Collections- Retrival from nested while loops
|
Saurabh KumarD
Greenhorn
Joined: Jul 23, 2009
Posts: 12
|
|
Hi Please help me solving following problem.
I have 2 Lists.
Say List A and List B.
In List A there are 11 elements and in List B there are 10 elements.
In List 10 elements are having a property which matches properties of 10 elements in List B respectevly.
And one element in List A is extra.
But the problem is that extra come in the middle say 5th element.
So whats hapeening when I am iterating both the list List A as outer and B as Inner, when the A comes to 5th element it ingners List B and moves ahaed . and does fine for rest.
So the problem is even 5th ele which is coming last in List A is never get printed even though it has corresponding matching in List B, because at that time List B is exhausted.
Please help its very urgent for me.
|
 |
Aurelian Tutuianu
Ranch Hand
Joined: May 13, 2004
Posts: 86
|
|
|
Can you give us some code?
|
http://javasign.blogspot.com/
|
 |
Saurabh KumarD
Greenhorn
Joined: Jul 23, 2009
Posts: 12
|
|
List<a> A= some code here to retrive List A --- A has 11 elements
List<b> B= some code here to retrive List B --- B has 10 elements
Iterator<a> ItrA=A.iterator();
Iterator<b> ItrB=B.iterator();
int count=0;
while(A.hasNext()){
a=A.next();
System.out.println("Some Label corresponding to A" + a.someLabel);
while(B.hasNext()){
b= B.next();
System.out.println("Inner While Loop Count" + ++count);
System.out.println("Some ID for Corresponding Value in A"+ a.someGetID());
System.out.println("Some ID for Corresponding Value in B"+ b.someGetID());
// Apart from 1 problamatic case these ID's are equal
if(a.someid().equals(b.someid())){
Do something;
}
break;
}
}
Sample Output -
Label 0
Inner While Loop Count1
ID in A - 000
ID in B - 000
***********************************************************************************
Label 1
Inner While Loop Count2
ID in A - 001
ID in B - 001
***********************************************************************************
Label 2
Inner While Loop Count3
ID in A - 002
ID in B - 002
***********************************************************************************
Label 3
Inner While Loop Count4
ID in A - 003
ID in B - 003
***********************************************************************************
Label 4
Inner While Loop Count5
ID in A - 004
ID in B - 004
***********************************************************************************
Label 21
Inner While Loop Count6
ID in A - 021
ID in B - 005
***********************************************************************************
Label 6
Inner While Loop Count7
ID in A - 006
ID in B - 006
***********************************************************************************
Label 7
Inner While Loop Count8
ID in A - 007
ID in B - 007
***********************************************************************************
Label 8
Inner While Loop Count9
ID in A - 008
ID in B - 008
***********************************************************************************
Label 9
Inner While Loop Count10
ID in A - 009
ID in B - 009
***********************************************************************************
Label 5
|
 |
Aurelian Tutuianu
Ranch Hand
Joined: May 13, 2004
Posts: 86
|
|
If the lists are not ordered you should reinitialize iterator of the inner list inside of the loop of the outer list.
As a sample you put line before the line .
If the lists are ordered you can try to pass once through both lists and advance at every step into the list which has smaller current element.
|
 |
Saurabh KumarD
Greenhorn
Joined: Jul 23, 2009
Posts: 12
|
|
Could you please illustrate what to do for ordered list by using some sample code.
Thanks please
|
 |
Aurelian Tutuianu
Ranch Hand
Joined: May 13, 2004
Posts: 86
|
|
Hope it works, I never run it. Just to have an idea.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
|
"S Db" please read the important administrative private message I just sent you.
|
 |
 |
|
|
subject: Java Collections- Retrival from nested while loops
|
|
|