Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

For loop seems to be running more than necessary in Java

for(LogProcess process : lo) {
  for(PathInfo info : process.getPathinfolist()) {
    a1.add(info.getFilepath());
  }
}
I have this piece of code here. lo is an instance of a LinkedList of size.

The LinkedList is of:
public LogProcess(int rev, String auth, String date,
                  List<PathInfo> pathinfolist)

Open in new window


Now PathInfo is of:
public PathInfo(String action,String kind,String filepath)

Open in new window

Now my LinkedList size is 22. And that is fine.

But process.getPathInfoList() is reading 328 records from xml. However after the loop when I check the size of a1 using a1.size() it shows 7216 which is impossible. Where am I wrong?

I agree the loop is executing 328*22 times but still when process.pathinfolist is only of size 328,how can a1.size() be 7216?
0
akashsuresh
Asked:
akashsuresh
  • 3
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I agree the loop is executing 328*22 times but still when process.pathinfolist is only of size 328,how can a1.size() be 7216?

I don't know why you are questioning this, as you seem to already realise that the loop is executing 7216. The a1.add call is inside this nested loop so why would it NOT add 7216 items to a1?

Do you understand what happens with nested loops? You seem to have some idea as you say you 'agree the loop is executing 328 * 22 times'. But think about it as the program would run... The on entry in the 1st for loop, process is the first LogProcess object (out of 22) from the linked list. You are then saying to loop through all items in process.getPathinfolist (328 of them) and for each one add it to a1. So after this a1.size() == 328. THEN, control returns to the outer for loop where the 2nd process from the LogProcess linked list is considered. For this process, process.getPathInfoList() is called and for each item in that list (assuming 328), each one is added to a1, and so a1 now holds 656 items, etc, etc, etc.
0
 
akashsureshAuthor Commented:
I understand that.But how do I get a list with only 328 elements?
0
 
akashsureshAuthor Commented:
And I want to clarify one more thing.The number of elements in pathinfolist is not the same for all objects of lo.

Some have 1 element,some have 5 and some may have even 20.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
And I want to clarify one more thing.The number of elements in pathinfolist is not the same for all objects of lo.

Some have 1 element,some have 5 and some may have even 20.

Are you sure? Have you debugged to double check that process.getPathinfolist().size() is different for each of the 22 times around the outer loop? And not just assuming that that is the case?       If so, can you post more of your code, because the code you have above lines up with that getPathinfolist being 328 every time through the outer loop.
0
 
akashsureshAuthor Commented:
Your statement seems justified to me.The code for xml parsing was written by some one else.Now on cross check I see that all tags with one name have been put int every object instead of the respective ones.You are right.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now