• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Sam Liew
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so now you need to connect these two ideas. Classname -> ArrayList
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 56541
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56541
172
  • Mark post as helpful
  • send pies
  • 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<>()
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!