• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

(homework) What does creating an arraylist object in a constructor mean?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Question:

Given the following BlueJ class diagram



Lecturer class (same with previous lab, no changes needed)
Student class (same with previous lab, no changes needed)

LectureRoom (changes occurs here)
1. LectureRoom has roomNumber (e.g. A301), courseName (e.g. Java), lecturer (a reference to a Lecturer object), and studentList (a reference to an ArrayList that stores Student object).
2. LectureRoom has a constructor that receives courseName, roomNumber, and Lecturer. The constructor then sets/assign the courseName, roomNumber and Lecturer. This constructor also creates the studentList arraylist object.


I am fairly new to Java programming and I think I got Question 1 Correct.

It is the second question that really made my grey hair blossom. I did my search first but I can't seem to find any similar question.

Could anyone kindly explain to me? Samples would be greatly appreciated!

Sam
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList is an object. How do you create objects in Java?
How do you assign an object reference to the variable?
The Java™ Tutorials
 
Sam Liew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:ArrayList is an object. How do you create objects in Java?
How do you assign an object reference to the variable?
The Java™ Tutorials




 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so now you need to connect these two ideas. Classname -> ArrayList
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you can work out how to initialise that List variable. I suggest you should initialise every instance field in the constructor. The syntax you used will not work; you must not use the type in the constructor, otherwise you re‑declaring it.
No, do not use objectname.variablename = value because all variable names should have private access.
 
Sam Liew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

I presume you can work out how to initialise that List variable. I suggest you should initialise every instance field in the constructor. The syntax you used will not work; you must not use the type in the constructor, otherwise you re‑declaring it.
No, do not use objectname.variablename = value because all variable names should have private access.



Thanks!

I don't get what you mean by initializing every instance field in the constructor. I mean, all the instance fields ( courseName, roomNumber , Lecturer ) are inside the constructor

I think the question "This constructor also creates the studentList arraylist object. " means initializing the ArrayList in the constructor?

i.e:



Could this be it?
 
Sam Liew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Ok, so now you need to connect these two ideas. Classname -> ArrayList



Err does "This constructor also creates the studentList arraylist object." mean connecting class with the ArrayList object that I create? ( i.e studentList )
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Liew wrote: . . .


Could this be it?

No. You have made the mistake of moving the List into the constructor, where it becomes a local variable in the constructor. You need to put it back in the class, with private access (as you had earlier), and then assign that variable to a new List in the constructor.
 
Sam Liew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

No. You have made the mistake of moving the List into the constructor, where it becomes a local variable in the constructor. You need to put it back in the class, with private access (as you had earlier), and then assign that variable to a new List in the constructor.



Hi I think I got it right this time.





Is this the proper method?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Liew wrote: . . . Hi I think I got it right this time. . . .

Yesyesyesyesyesyesyes

Two enhancements I would suggest.
  • 1: Find out about “program to the interface” and you will than know it is better to declare it as List<Student> myList rather than ArrayList
  • 2: If you can predict the number of items to add, you can get slightly faster execution if you specify that: new ArrayList(1000);
  • You need this. for the other three fields; you may miss it out for the List because there is nothing in the constructor it could be confused with.
    In Java7+ you can write new ArrayList<>()
     
    Right! We're on it! Let's get to work tiny ad!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic