aspose file tools*
The moose likes Beginning Java and the fly likes how to Initialize an ArrayList in a constructor with no parameters? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to Initialize an ArrayList in a constructor with no parameters?" Watch "how to Initialize an ArrayList in a constructor with no parameters?" New topic
Author

how to Initialize an ArrayList in a constructor with no parameters?

Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
The following code represents the variables of a class.My problem is that i dont know how to use the ArrayLists on my constructors.


What i need to add is that the ArrayLists are initialized at the constructor above,but i dont know how to.....
Please help
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Stamatis Samaras wrote:this(0,"","",0.0,"","");

You have another constructor that initializes all the members, when passed those values?! Think so.
Just dont initialize them in the Class, but pass 'new ArrayList()' at appropriate places to your constructor.
Like,

Please use code tags around your code while posting, as that differentiates your code from plain text.

and Stamatis, welcome to JavaRanch


OCPJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Welcome to the Ranch

You have several things which ought to be improved. Don't declare ArrayLists at all, but declare them as Lists. Make sure you have an actual type parameter (generics) on the left of the = and on the right of the =.
Why are you using this(0, "", "", 0.0, "", "");? It appears to me that those zeroes and empty Strings suggest that constructor is unneeded, and getting rid of that constructor altogether might be a good idea.
If you insist on that constructor with the 0 values, you can put the initialisation inside the other constructor.

But why are you worrying about initialisation at all? You are initialising the Lists where you declare them.

I would write a class like this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Vinoth Kumar Kannan wrote: . . . pass 'new ArrayList()' at appropriate places to your constructor. . . .
Why?

That can far better be written inside the other constructor. In fact you probably don't want to pass those Lists as constructor arguments, otherwise you will get Lists passed from elsewhere which can be altered in this class, leading to all sorts of strange errors.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Vinoth Kumar Kannan wrote: . . . Please use code tags around your code . . .
Good point; thank you for noticing. Since Stamatis Samaras is new, I shall add the code tags for him and he can see how much better the post looks.
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
Vinoth Kumar Kannan wrote:
Stamatis Samaras wrote:this(0,"","",0.0,"","");

You have another constructor that initializes all the members, when passed those values?! Think so.
Just dont initialize them in the Class, but pass 'new ArrayList()' at appropriate places to your constructor.
Like,

Please use code tags around your code while posting, as that differentiates your code from plain text.

and Stamatis, welcome to JavaRanch


Well thanks about all but this way it isnt working
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
Campbell Ritchie wrote:Welcome to the Ranch

You have several things which ought to be improved. Don't declare ArrayLists at all, but declare them as Lists. Make sure you have an actual type parameter (generics) on the left of the = and on the right of the =.
Why are you using this(0, "", "", 0.0, "", "");? It appears to me that those zeroes and empty Strings suggest that constructor is unneeded, and getting rid of that constructor altogether might be a good idea.
If you insist on that constructor with the 0 values, you can put the initialisation inside the other constructor.

But why are you worrying about initialisation at all? You are initialising the Lists where you declare them.

I would write a class like this

Thanks about this too but i need 2 constructors ,the second will be like the one you posted on the code sample but actually i allready knew how to do that.The 1st constructor is one with no parametres and i cant just leave it like this


because errors pop up

I need the constructor with no parametres because it serves as a template for creating the "empty" space on a random access file where my data are going to be saved.I hope you understand me .Thanks again and any further help is going to be appreciated.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60076
    
  65

Stamatis Samaras wrote:thnx about this ...

Please use real words when posting to the forums. Abbreviations such as "thnx" in place of "thanks" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please click this link ⇒ UseRealWords for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
Bear Bibeault wrote:
Stamatis Samaras wrote:thnx about this ...

Please use real words when posting to the forums. Abbreviations such as "thnx" in place of "thanks" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please click this link ⇒ UseRealWords for more information.


I am really sorry about this but i am desperate due to a deadline for my project so i had no time to read rules and templates for proper writing at this forum.Thanks for telling me the correct way i wont use any more abbreviations on future posts.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60076
    
  65

No need to apologize -- the guideline is there to help you get the most out of JavaRanch.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
If you have a constructor with parameters, you can call it with default values from another constructor with this( . . . );. All the constructor logic can go in the parameterised constructor.

The bit about "empty space" in a random access file worries me. No, I don't understand it. I am not sure it is good design.

You say "errors occur"? What sort of error; nobody can help if you simply say "it isn't working". If it si a compiler error, it may be that your this() call does not match the constructor parameters.
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
Well actually i was missing something the first time i used this :

but it actually works with no errors from compiler but the next problem that pops up and i am afraid that this isnt going to be solved ,because my design seems weak to dynamic arrays ,is that i need a standard size of the variables i use in the constructor so although the raw type variables provide that to me (1 int=4byte,4string x20bytes each(i used a method to control their size)and one double with 8 bytes makes it 92 bytes) the dynamic arrays dont have a standard size which makes me wonder what i am going to do next? Any ideas?
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
The bit about "empty space" in a random access file worries me. No, I don't understand it. I am not sure it is good design.



I have a class that creates a random access file with 100 blank records .Each blank record is an instance of the class of the constructor with no parameters above.So every instance of the class contains a 0 for the int, null for every string and 0.0 for the double type (and also something for the arraylists that i have no idea what is used for this type and i hope to find a solution,i havent done anything for the arraylists yet just some random ideas tests that doesnt seem to work as i would like to but it works pretty good for the raw types so far).So eventually this class creates the proper "empty" space for saving the data.I hope i helped you understand this part of my design.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Stamatis Samaras wrote: . . . null for every string . . .
What nulls? You aren't passing any null Strings (thank goodness). You are passing 0-length Strings. It still appears to be inappropriate design for a random-access file, which requires a set length for every entry; Strings with content in will occupy a different length from 0-length Strings and will put all the subsequent records out of phase.

There is an example of RandomAccessFile in use in H M Deitel and P Deitel, Java, How to Program, but only in the 6th and earlier editions (I think).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Even if you always use the same space for your Strings, those arrays will make random access very difficult. Unless you have been told to use a random access file, you might do well to consider another type of file.

. . .

Or serialising the whole object.
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
Campbell Ritchie wrote:
Stamatis Samaras wrote: . . . null for every string . . .
What nulls? You aren't passing any null Strings (thank goodness). You are passing 0-length Strings. It still appears to be inappropriate design for a random-access file, which requires a set length for every entry; Strings with content in will occupy a different length from 0-length Strings and will put all the subsequent records out of phase.

There is an example of RandomAccessFile in use in H M Deitel and P Deitel, Java, How to Program, but only in the 6th and earlier editions (I think).


I think i made it even more confusing to you.I dont pass any null or zero.As I said this is just a template which is going to be filled with real data numbers and strings that are fixed to 20 chars length.I dont think i can complete this with dynamic arrays i may switch it to a standard array with 20-50 blocks for save.I wish i could have more time to rethink my design.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
No, you are not passing nulls, thank goodness, which might cause no end of problems.

That constructor call, however, does include 0-length Strings ("") and 0 or 0.0 numbers.
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
Campbell Ritchie wrote:No, you are not passing nulls, thank goodness, which might cause no end of problems.

That constructor call, however, does include 0-length Strings ("") and 0 or 0.0 numbers.


As i said the raw variable types work 100% fine and make advantage of random access file with no problems.It is tested and it runs 100% ok ,but the thing that i have to add a dynamic array-like database to save multiple strings and integers to it without of course knowing how many strings or how many integers the user is going to give makes me feel sick about java...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Yesterday, I wrote: . . .
I would write a class like this
Let's update it a bit, by adding a no-args constructor . . . and there you have a List being instantiated. That will work for both constructors.
Stamatis Samaras
Greenhorn

Joined: Aug 24, 2010
Posts: 13
Campbell Ritchie wrote:
Yesterday, I wrote: . . .
I would write a class like this
Let's update it a bit, by adding a no-args constructor . . . and there you have a List being instantiated. That will work for both constructors.


I think i can make use of a class like this ,its most likely the answear to my problem ,i really liked the part .Thanks about everything ,even if i still dont make any progress due to a bad design perhaps .I appreciate everything ,you are indeed brilliant
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Stamatis Samaras wrote: . . . i really liked the part .Thanks about . . .
If I were a real cowboy, I'd say
Aw shucks, you've got me all embarrassed
but I'm not a real cowboy, so . . .

You're welcome and good luck with the project.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to Initialize an ArrayList in a constructor with no parameters?
 
Similar Threads
Need help with using arraylist
want to make a table~~~
ArrayList
How can I manually add values to ArrayList[]
How do I declare an arrayList and initilise it in a constructor Method