Results 1 to 12 of 12
Thread: Arraylist help.
- 12-23-2010, 12:40 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Arraylist help.
Hello, I created an Arraylist to hold multiple classrooms. However I'm stuck with how to add a teacher in my School class, to a classroom by using the classrooms arraylist parameter (such as 0,1,2 etc)
Java Code:public class classroom { private String classRoomName; private String teacherName; public void setClassRoomName(String newClassRoomName) { classRoomName = newClassRoomName; } public String returnClassRoomName() { return classRoomName; } public void setTeacherName(String newTeacherName) { teacherName = newTeacherName; } public String returnTeacherName() { return teacherName; } }
Java Code:import java.util.ArrayList; public class School { private ArrayList<classroom> classrooms; private String classRoomName; private String teacherName; public School() { classrooms = new ArrayList<classroom>(); } public void addClassRoom(classroom newClassRoom, String theClassRoomName) { classrooms.add(newClassRoom); classRoomName = theClassRoomName; } // how to write a method to add a teacher to the classroom by using the classroom parameter // and the teachers name }
- 12-23-2010, 02:40 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do you want to keep a map with the classroom and teacher assign to it?
- 12-23-2010, 03:02 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
- 12-24-2010, 12:45 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
array 0 means that you want to add in a specific index of the ArrayList? Could you please clarify that.
Here in the following code segment,
you've added classroom object to the end of the ArrayList always.Java Code:public void addClassRoom(classroom newClassRoom, String theClassRoomName) { classrooms.add(newClassRoom); classRoomName = theClassRoomName; }
- 12-24-2010, 01:27 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Yup, I want to add a teacher to a specific index of an Arraylist.
So e.g. I create.
0 - History Classroom
1 - Maths Classroom
2 - Science Classroom
And I would like to add Mr Puchatek to 2 the science class.
Sorry I'm very bad at explaining what I need lol.
Thanks, Puchatek
- 12-24-2010, 01:32 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Then look at add(Object obj, int index) in array class.
I hope the above three are different arrays, is it?0 - History Classroom
1 - Maths Classroom
2 - Science Classroom
- 12-24-2010, 11:54 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
- 12-25-2010, 12:43 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Same as follows,
additionally you've to specify the index you want to store the object.Java Code:classrooms.add(newClassRoom);
Java Code:classrooms.add(newClassRoom, 1); classrooms.add(newClassRoom, 2);
- 12-25-2010, 12:44 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-25-2010, 12:45 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Sorry about that I've added arguments in incorrect order. It should be like this,
Please check with the above link.Java Code:add(int index, Object obj);
- 01-02-2011, 01:16 AM #11
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Thank you for your help.
As I don't want to make another topic. I want to write a method in the School class to call the method setTeacherName in the classroom class. How do I do this?
Thanks
- 01-06-2011, 02:58 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Define a method in School class which you want to invoke by. Then create an object of Classroom class within it, and invoke the method, which is public.
Similar Threads
-
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
A private static ArrayList hold an object and static getter ArrayList
By Louis in forum New To JavaReplies: 2Last Post: 11-16-2010, 05:51 PM -
Creating an ArrayList from an ArrayList
By Klahking in forum New To JavaReplies: 17Last Post: 09-09-2010, 03:34 PM -
Help with ArrayList
By nura23 in forum New To JavaReplies: 4Last Post: 01-10-2010, 01:23 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks