• 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

Records in Arraylist

 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a small problem....

I have got an arraylist of records say course details of a student. The status of courses is different. e.g suppose he is taking Java course the status will be in progress...for someother course the status will be completed or scheduled, etc....

I need to show to the user count of courses in each status...how should i go abt it???

TIA

Grishma
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have an ArrayList containing the details of 'n' courses and these courses have their own arrtibutes (like course status ,dates etc.)

I guess you must be having a data structure something like CourseDetails with fields like status, etc.

You must iterate through the arraylist get each status and increase the count of this status. You can use a HashMap for this purpose.

The Key is the Course status/status code and the value is the count(no. of times it has occured).
 
Grishma Dube
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Ragu...

I will be grateful to you, if you can explain me the sequence of activities..

Grishma
 
Ragu Ram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • Iterate through the ArrayList you have.
  • Get the CourseDetails object from the list and read the status code
  • Check if this Key(The retrieved status code) is already present in the hash map. (You can do that using the HashMap.containsKey(Object key) method).
  • If No add this as a Key to a HashMap and set the value as '1'
  • If Yes get the value corresponding to the Key increment it by one and save it back into the hash map using hashmap.put.
  • After iterating over the arraylist your hashmap will contain the status code and the number of occurences.
  • Use HashMap.keySet to get the list of keys. Iterate over them and get the values (the number of occurences).


  • There could be other solutions but this one just struck my mind. I am not sure if jdk 1.5 has some thing which can make this very simple.
     
    Ragu Ram
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I forgot to mention. You dont have to be grateful to me. Just a thanks will do.
     
    Grishma Dube
    Ranch Hand
    Posts: 275
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanx a lot for the info...

    it helped me lot...

    Grishma
     
    reply
      Bookmark Topic Watch Topic
    • New Topic