aspose file tools*
The moose likes Java in General and the fly likes Count item elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Count item elements" Watch "Count item elements" New topic
Author

Count item elements

S Roberts
Ranch Hand

Joined: Dec 06, 2013
Posts: 66
i need to get the count of the items elements in each item of the list. For example i have one group list in this list are 5 elements, and on each elements there are also some other items. I need to get these count of the items on each elements list to show them nearby elements name in brackets like:


I have a list of the group, then i add elements with the name and id to this list, like:


how can i get the count of each item elements that contains in p? Any suggestions

I have one list of Group


each group item has its own elements


i want to get the count of the last tree elements to show on the Group1 (4) for example

i have two methods in one of them i call the


and get the number of elements that i want but on console, if i call this variable in the second method is it null,


how to call it in the second method, i defined it as static

private static int number;
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
Not sure I understand the question.
You should not have static anything if you want to count instances. It should be very easy to iterate a List<List<Foo>> and count the elements with count += something() or to print out some details from the List and its size. What is going wrong that it seems so complicated? I presume you are familiar with the methods of the List interface? And the Java Tutorials section?
Of course in Java8 you would use a Stream. Try
myList.stream().sum(List::something)
and then you can see how many errors I have made
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
I am 99.9% sure the stream code I showed will turn out to be incorrect.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

S Roberts wrote:i need to get the count of the items elements in each item of the list. For example i have one group list in this list are 5 elements, and on each elements there are also some other items. I need to get these count of the items on each elements list to show them nearby elements name in brackets like: ... [EXAMPLES DELETED]


In general, for simple groups within groups, you can just use nested loops to count the elements.

For larger number of groups within groups, or more complex setups, where the depth is unknown or the depth is different per group element, it is probably better to just use recursion.

For me, I would rather use recursion, even for the simple case, if there is a chance that the more complex case can happen in the future.

And finally, if there is some sort of loop in the grouping -- ie. a group has an element that is a parent group -- then you also need to have some sort of flag to prevent endless recursion.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
S Roberts
Ranch Hand

Joined: Dec 06, 2013
Posts: 66
thank you all for answering, with the nested loop for for(Group p: list) { l.add(p.getName()+ "("+number+")",p.getId());} how can i add the second loop hier? it means it must loop for each element in p, can you please provide me some sample code?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

S Roberts wrote:thank you all for answering, with the nested loop for for(Group p: list) { l.add(p.getName()+ "("+number+")",p.getId());} how can i add the second loop hier? it means it must loop for each element in p, can you please provide me some sample code?


The Group class is something that you wrote. Or if it isn't, and it came from somewhere, you didn't provide any detail on what it is.... How can we provide sample code for using something that you wrote? And when we don't know anything about it? ... Not to mention why would you need samples on using your own class?

Henry
S Roberts
Ranch Hand

Joined: Dec 06, 2013
Posts: 66
i create the chart in on



i can't understand how to call this chartAnzahl in the onSuccess method, to show the count of this item elements within the elements of the list
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
Is chartAnzahl a field?

There is something which sounds like poor design in this. You need to consider what sort of chart you are drawing. Do you have objects representing rows? Can you get a count from those objects?
 
 
subject: Count item elements