Results 1 to 7 of 7
- 02-19-2011, 07:41 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
Adding to a attribute of an object in a map
If I had a HashMap
Where Pupil is an Pupil object from another class which contains an array attribute called grade.Java Code:[/HTML]this.pupils = new HashMap<String, Pupil>();
And then i had a forEach loop within a method. Part of the method is below.
I'm stuck as how to assign score to each of the objects array attribute grade.Java Code:if(test == true) { for(String name : pupils.keySet()) { score = 76; } }
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.
-
Since you have the key, you'd use the get method on the Map.
- 02-19-2011, 08:55 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
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?
-
- 02-19-2011, 09:04 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
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.
-
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); }
- 02-19-2011, 09:21 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
yes
yes
is it an array of int? an int? if an array of int, does each value need to be 76?
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
-
adding or removing an object from an array of objects
By javanew in forum New To JavaReplies: 1Last Post: 05-04-2010, 11:00 AM -
Adding functions to existing Object
By ThommyW in forum Advanced JavaReplies: 8Last Post: 03-25-2010, 10:21 AM -
Send object via web service, with a java.sql.Timestamp attribute
By diegofsza in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 08-15-2009, 11:40 PM -
Adding elements to an Object Array
By aneesahamedaa in forum New To JavaReplies: 4Last Post: 09-07-2008, 03:55 PM -
Adding listener to non-Java object?
By cruxblack in forum Advanced JavaReplies: 5Last Post: 07-30-2007, 02:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks