Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList question

 
Jacob Hoda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic