• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic