This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ArrayList question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList question" Watch "ArrayList question" New topic
Author

ArrayList question

Jacob Hoda
Greenhorn

Joined: Apr 09, 2004
Posts: 6
I have an array list setup in my panel, I have another class that will do the adding to list on the panel.

In that second class how would I call and add items to the Arraylist on the panel without instantiating a new panel everytime?

Am I able to just refer to that arraylist and start adding?

Thanks.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
If I understand your question, there are many ways in which you could do this:

1) In the panel that has the ArrayList, put in a getter method so that the *other* code outside the panel can *get* that ArrayList...

class MyPanel ... {
// stuff
public ArrayList getTheArrayList() {
return theArrayList;
}
}

2) You could create a method in the *other* code that *accepts* an ArrayList, and then pass the ArrayList to that other code, and allow that other code to then populate that ArrayList...

class MyOtherCodeThatFillsArrayList {
pubic void fillArrayList(ArrayList list) {
// do stuff to fill up the ArrayList
// you do not have to return the ArrayList, since you have passed a reference to the
// One True ArrayList, so when the panel uses it again, it is changed.
}
}

If you *have* learned about polymorphism, then consider making your references of type List instead of an ArrayList, while the actual objects will be of type ArrayList (if you haven't learned about polymorphism yet, do not worry about this yet!!)

There are other issues related to best practices, but there is no need to worry about that right now--I assume that if you are at the beginning level, you are still trying to figure out how all the pieces fit together and work...

cheers,
Kathy
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jacob Hoda:
I have an array list setup in my panel, I have another class that will do the adding to list on the panel.

In that second class how would I call and add items to the Arraylist on the panel without instantiating a new panel everytime?

Am I able to just refer to that arraylist and start adding?

Thanks.


I've encountered this situation in many programs I've written. My typical solution may not be the best, but basically, I pass a reference to the panel as an argument to the constructor of the other class. The panel then provides getter and/or setter methods to perform the appropriate actions. It might look something like this:


In fact, this idiom can be used whether or not you are doing GUI programming. Please feel free to modify this to fit your needs. Alternatively, you can pass a reference to the ArrayList directly to the OtherClass constructor.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList question
 
Similar Threads
Printing the ArrayList
behaviour of add() of ArrayList
what will happen while adding already existing panel in JTabbedPane?
Using EL to retrive data from Arraylist
Vectors/Arrays/List?