PDA

Click to See Complete Forum and Search --> : [RESOLVED] arraylist question


MoE70
Mar 18th, 2010, 10:16 AM
I want item to add itself to the array list in Library automatically when item (book/film,music) is created. how can i do that?

kfcSmitty
Mar 18th, 2010, 10:31 AM
First you will need a class with a main method so you can run your program.


public class Main {
public static void main(String[] args) {

}//end main
}//end class


Then you will need to create an instance of your library class

Library library = new Library();


Then you will need to call the addItem method with the object you wish to add.


library.addItem(new Film());


Since "new Film()" is calling the constructor of your Film() class, you will need to pass it the appropriate parameters.

MoE70
Mar 18th, 2010, 03:05 PM
Isn't it possible to have something like this in the item class or film class so that when a film object is created it automatically adds it self to the array list? is that possible at all?

library.addItem(this film) (pseudo)

because im using bluej and it doesn't require main methods. its supposed to be for beginners...

kfcSmitty
Mar 18th, 2010, 06:58 PM
If you pass the constructor the arraylist as a parameter, you could add it easily, yes.

But it is bad practice to do something like that. If your arraylist is in one class, your methods from another class should simply return data to enter into the arraylist.

Alternatively you could send each method the arraylist as a parameter and then set the arraylist to the return value of the method.

MoE70
Mar 19th, 2010, 06:54 AM
how do i do any of those?

kfcSmitty
Mar 19th, 2010, 07:50 AM
I'm just looking at your code again, and I am wondering why you would want to do it that way. Could you explain what you're attempting to achieve?

You specifically have a method in your Library class for addItem, which adds it to the arraylist, so why would you want that private variable to be accessible from the other classes?

MoE70
Mar 19th, 2010, 07:57 AM
because at the moment on bluej, I create a Library object then I create a Book object and then I call the addItem() to add that book object so I was wondering if it'd be possible to have the last done automatically.

By the way in bluej to create an object, you right click the class from a class diagram and you click 'create new...' so the creation of objects and method calls are done with the mouse no by code.

kfcSmitty
Mar 19th, 2010, 08:25 AM
I'm not sure how bluej works, but why don't you call the addItem and create the book object at the same time(like my above example), instead of doing each one separately?

The only way I can think of achieving what you want is using a singleton design pattern. That way you could declare an instance of your Library class in each method and append the arraylist, but it seems like an ugly way to accomplish it.

Using the singleton method, you would need to create your object, and then call methods to append the array. One benefit would be that you don't need to instantiate the class in your main method.

So to achieve that, you would need to modify your library class to include the below code. Notice that the constructor is changed to protected. The rest is to ensure there is only one instance of the library.


private static Library instance = null;
protected Library() {
al = new ArrayList<item>();
}
public static Library getInstance() {
if(instance == null) {
instance = new Library();
}
return instance;
}


Then, in each of your methods, you would need to have something like


Library library = new Library();
library.addItem(this);


In your particular case, it would go in the constructor.

I stand by this being a bad idea, though.

ComputerJy
Mar 19th, 2010, 12:35 PM
I'm not sure how bluej worksWell, BlueJ is a really cool OO IDE. You design your classes and instead of having code control class instantiation and method calling you can do it yourself.

Anyone trying to learn Object Oriented should definitely use it.

MoE70
Mar 21st, 2010, 03:13 PM
It looks I should just leave it as it is then. Thanks smitty