Results 1 to 7 of 7
  1. #1
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default Adding to a attribute of an object in a map

    If I had a HashMap

    Java Code:
    [/HTML]this.pupils = new HashMap<String, Pupil>();
    Where Pupil is an Pupil object from another class which contains an array attribute called grade.

    And then i had a forEach loop within a method. Part of the method is below.

    Java Code:
       if(test == true)
             {
               for(String name : pupils.keySet())
               {
                score = 76;
               
             
             }
             }
    I'm stuck as how to assign score to each of the objects array attribute grade.
    Logic to me says i want to be using a pupils.put() command but i am struggling to work out how to make it access the grade array within the pupil object within the map.

    Any possible pointers?
    Cheers.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    So make it a two stage process, first get the object then use the put command?

    Would you assign the get statement to a variable?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javadingbat View Post
    So make it a two stage process, first get the object then use the put command?

    Would you assign the get statement to a variable?
    I'm not sure what you're doing though and why. Can you elaborate on the details of your problem? You can iterate through the values held in the map without extracting the key set if need be.

  5. #5
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    The problem is how to syntactically assign the score of 76 to each array of a pupil object. When the pupil object is part of a hashMap called pupil.

    eg

    forEachPupilInThePupilMap.get(grade array);
    forEachPupilinThePupilMap.put(grade array = 76);

    I know that code isn't correct but i'm just trying to show the process i'm trying to achieve. I'm just looking for the example of the logic of how i would do such a thing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    So each Pupil object has a gradeArray field? is it an array of int? an int? if an array of int, does each value need to be 76?

    if an int and you have a setGradeArray method, then iterating through the values could work:

    Java Code:
          for (Pupil pupil : pupils.values()) {
             pupil.setGradeArray(76);
          }

  7. #7
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    So each Pupil object has a gradeArray field?
    yes


    is it an array of int? an int? if an array of int, does each value need to be 76?
    yes

    Without writing the separate method. For example you assign a instance variable by using the format

    type of variable, variable name, = variable value.

    Can you show the logic of how you structure it if

    HashMap = Pupils
    Array attribute within Pupils name = grade
    score = 76

Similar Threads

  1. Replies: 1
    Last Post: 05-04-2010, 12:00 PM
  2. Adding functions to existing Object
    By ThommyW in forum Advanced Java
    Replies: 8
    Last Post: 03-25-2010, 11:21 AM
  3. Send object via web service, with a java.sql.Timestamp attribute
    By diegofsza in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 08-16-2009, 12:40 AM
  4. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 04:55 PM
  5. Adding listener to non-Java object?
    By cruxblack in forum Advanced Java
    Replies: 5
    Last Post: 07-30-2007, 03:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •