• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Count item elements

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am 99.9% sure the stream code I showed will turn out to be incorrect.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
S Roberts
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic