Results 1 to 12 of 12

Thread: Arraylist help.

  1. #1
    puchatek is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default 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
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    puchatek is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Do you want to keep a map with the classroom and teacher assign to it?
    Not for now. I just want to add a teacher to the correct array e.g. array 0.
    Im still very new to java lol
    Thanks

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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,

    Java Code:
    public void addClassRoom(classroom newClassRoom, String theClassRoomName)
        {
            classrooms.add(newClassRoom);
            classRoomName = theClassRoomName;
        }
    you've added classroom object to the end of the ArrayList always.


  5. #5
    puchatek is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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,

    Java Code:
    public void addClassRoom(classroom newClassRoom, String theClassRoomName)
        {
            classrooms.add(newClassRoom);
            classRoomName = theClassRoomName;
        }
    you've added classroom object to the end of the ArrayList always.

    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

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Then look at add(Object obj, int index) in array class.

    0 - History Classroom
    1 - Maths Classroom
    2 - Science Classroom
    I hope the above three are different arrays, is it?


  7. #7
    puchatek is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Then look at add(Object obj, int index) in array class.



    I hope the above three are different arrays, is it?

    Yup they are different arrays.

    How would I have to write?
    Java Code:
    add(Object obj, int index)
    to add a teacher?

    Thanks

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Same as follows,

    Java Code:
    classrooms.add(newClassRoom);
    additionally you've to specify the index you want to store the object.

    Java Code:
    classrooms.add(newClassRoom, 1);
    classrooms.add(newClassRoom, 2);

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Sorry about that I've added arguments in incorrect order. It should be like this,

    Java Code:
    add(int index, Object obj);
    Please check with the above link.

  11. #11
    puchatek is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    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

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  2. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  3. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 03:34 PM
  4. Help with ArrayList
    By nura23 in forum New To Java
    Replies: 4
    Last Post: 01-10-2010, 01:23 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 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
  •