Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: ArrayList as a parameter

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList as a parameter

    I have five classes all together: Instructor, Student, Course, Section, and Testing.

    In the section class, I have this:

    public class Section {
        private Course course;
        private Instructor instructor;
        private ArrayList<Student> students;
     
        public Section() {
            super();
        }
     
        public Section(Course course) {
     
        }
     
        public Section(Course course, Instructor instructor) {
     
        }
     
        public Section(Course course, Instructor instructor, ArrayList<Student> students) {
     
        }
          // getters and setters
     
        public void setCourse(Course course)
          {
            this.course = course;
          }
     
        public Course getCourse()
          {
            return course;
          }
     
        public void setInstructor(Instructor instructor)
          {
            this.instructor = instructor;
          }
     
        public Instructor getInstructor()
          {
            return instructor;
          }
     
        public void setStudents(ArrayList<Student> students)
          {
            this.students = students;
          }
     
        public ArrayList<Student> getStudents()
          {
            return students;
          }
     
        public void addStudent(Student student)
          {
            students.add(student);  // add a student to the section roster
          }  
        }
    In the main method, I want to create a Section object by accessing this constructor from the Section class:


    public Section(Course course, Instructor instructor, ArrayList<Student> students) {
     
    }

    So I started with:


    public class Testing01 {
        public static void main (String [] args) throws IOException {
     
            Section sec = new Section("Java Programming", "George Jones", /* ArrayList variable here*/); {
     
    }
    }
    }

    I would like the constructor to create the ArrayList, but I can not figure out how to code the parameter. It's not in my reading and I have scoured the Internet for the answer with no results. I really want to comprehend this, so can anyone walk me through this? I would be most appreciative.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList as a parameter

    I would like the constructor to create the ArrayList, but I can not figure out how to code the parameter.
    What class is the constructor in? The Testing01 class does not have a constructor.
    What parameter does the constructor need to have to create the ArrayList?


    Are you asking how to pass a variable to a constructor? Put the name inside the ()s
    TheClass tc = new TheClass(theVariable);
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: ArrayList as a parameter

    Creating an ArrayList isn't much different from creating any other object (you're already creating a Section object). Just pick a constructor and call new Constructor(params). Available ArrayList constructors: JavaDoc - ArrayList.


    ex. ArrayList constructor with no parameters
    new ArrayList<Student>()

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList as a parameter

    Quote Originally Posted by Norm View Post
    What class is the constructor in? The Testing01 class does not have a constructor.
    What parameter does the constructor need to have to create the ArrayList?


    Are you asking how to pass a variable to a constructor? Put the name inside the ()s
    TheClass tc = new TheClass(theVariable);
    I might be using the wrong words. I have the constructor in the Section class:

    public Section(Course course, Instructor instructor, ArrayList<Student> students) {
     
        }

    Am I missing information in the constructor? Is there supposed to be information in between the brackets?

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList as a parameter

    Quote Originally Posted by helloworld922 View Post
    Creating an ArrayList isn't much different from creating any other object (you're already creating a Section object). Just pick a constructor and call new Constructor(params). Available ArrayList constructors: JavaDoc - ArrayList.


    ex. ArrayList constructor with no parameters
    new ArrayList<Student>()
    Am I not able to use the constructor in the Section class to create the ArrayList variable?

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: ArrayList as a parameter

    The Section class constructor creates a Section class object. To create an ArrayList object you need to use an ArrayList constructor.

    The section class constructor takes as a parameter an ArrayList object, which must be constructed using an ArrayList constructor.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ArrayList as a parameter

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/63536-arraylist-parameter.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList as a parameter

    The only reason I cross-posted is because no one answered on the other board. I waited for two days.

    The really horrible fact about this entire thing is that I had finished this, and it was working. Apparently, though, I was told that I did it the hard way and given some advice to change it. Now I am really confused and frustrated.

    I do apologize.
    Last edited by mysdyva; October 5th, 2012 at 03:00 PM.

Similar Threads

  1. Replies: 3
    Last Post: May 27th, 2012, 11:11 AM
  2. array as parameter
    By pjo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 16th, 2012, 07:27 PM
  3. constructor with an arraylist parameter?
    By ronimacarroni in forum Object Oriented Programming
    Replies: 5
    Last Post: March 5th, 2012, 04:42 PM
  4. Initialization parameter of servlets
    By rakesh in forum Java Servlet
    Replies: 3
    Last Post: April 13th, 2010, 03:14 AM
  5. How do you pass an Array as a Parameter?
    By Arius in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2010, 09:36 PM

Tags for this Thread