• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList question  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, i would like to introduce myself. I am a new member and a current first year computer science in a university, i am 25 years old and i am trying my best to get better in Java. I have been browsing the cloud for a lot of time and according to my observations this is the best forum place to get started. And yeah, i am not a native English speaker or writer, so i apologize for my mistakes in advance.

Problem Description
The purpose of this exercise is to program a small part of a system to be used to manage the circulation of journals in an organisation. The organisation wants to keep track of which employees can see which journal.
Exercise 1
The class Journal holds information about a journal:



Add an instance variable circulationList of type ArrayList<String> to the class. This list will hold the names of employees that can see the journal. Modify the constructor, so that the list is initialised.

Ok, so i know that i have to put an additional statement in the constructor "circulationList = new ArrayList<String>();" and here is the place where i am starting to have a mode of confusion. Simply because when i try to create an object of the Journal class into another class, i call that constructor with the new additional field "circulationList = new ArrayList<String>()" and i still have to pass just 4 values, like i do not have any for that new reference to an Arraylist object that i created. I just do not see any difference if i initialise it, as soon as i declare it, or the constructor must be "aware" of it in some way ?


 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post, which you should always used, and you can see how much better it looks. There is a serious error in that constructor as you have written it.

Try a method like thisThen you will see why you must initialise all fields. In my opinion, the constructor is the best place to initialise all instance fields, because the constructor ought to ensure the object is in a “consistent” state.

It would be better to put Employee objects into the List rather than Strings, but you may not have any option about that.
 
Iliya Belichev
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, i have this method added later, of course there is no way for you to actually know it, so i am just going to paste it here. I am aware of the idea that you initialise instant variables in the constructor. My thought is that when i am creating an instance of this class, i have only 4 fields showing in Eclipse, 3 for String, one for Integer and i do not have anything for ArrayList, later on i am adding a method that adds a String filename to that collection, but i just do not understand why we have to create the Arraylist object in the constructor with the java.lang.circulationList = new ArrayList<String>(); This is the example that we have in BlueJ, so its not something that i came up with randomly

now when i am previewing my opinion i can see that we have to do it, just because the Arraylist is a part of the java.lang package ? its a propasal, with some clear logic


 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iliya Belichev wrote: . . . This is the example that we have in BlueJ, so its not something that i came up with randomly . . .
There is a mistake in that example; it should not include java.lang. I am not sure whether you have copied it wrongly or whether your use of the Javadoc button has somehow altered it. I don't like BlueJ myself and I think it is a bad idea to start with IDEs; I think you are best running that code from the command line. Suggestion:
  • Open the command line/terminal
  • If you already have a java (or similar) folder, omit the next step
  • mkdir java
  • cd java
  • Copy all your files into that java directory.
  • Create a JournalDemo class with a main method in
  • javac JournalDemo
  • java JournalDemo
  • Now comment out the line 19 in the constructor and run the code again with the last two instructions. Then see what happens.

    Some more design. Have you been told to give the Journal class all those setXXX methods? That would imply you are going to change the name of your Journal. Why have you used a List when you are testing whether a recipient is already on the List? That is not what Lists are for. If you want one instance of each only, you are thinking of a Set. There is even a special kind of set which remembers the order things were added.
     
    Ranch Hand
    Posts: 99
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also take a closer look at line 40.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is even better than the error I noticed in the constructor.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!