• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing an arraylist from another class  RSS feed

 
Thomas Molloy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I am trying to create a search button that will search an ArrayList located in a different class for a certain item. I have been searching forums for the answer and think I'm on the right track but am still getting errors. In the first class named PropertyGUI I have private ArrayList called saleList as well as a getter (code included below)



Then in another class called HomeFrame I have the following code:




However, I'm getting errors saying it cannot find the saleList symbol. Have I done something incorrectly?
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside ActionListener do :

Now you can use saleList in for loop
 
Thomas Molloy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much kind sir!
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want access to a List outwith its owning object in the first place? I am not convinced that is good design. Keep the List inside your object. I haven't the time to explain it now, but there are four ways to return Lists some of which preserve the original from alterations. Any manipulation of that List will be reflected in its original owning object, and that may constitute incorrect design.
 
Thomas Molloy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I am not quite sure what you mean.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have a bit more time:
I can think of four ways to return such a List, but you can't return “ArrayList”. You have to declare the method as returning List, and the recipient variable is declared as List, too. That is because some of those methods return a different sort of List from ArrayList.The following four methods are all in the class which myApp is an instance of:-Note that since Java7 it has been possible to miss out the actual type for generics; you can often simply write <>.
In version 1 you are returning a reference to the actual List. That means any code can alter the List without your being aware of it. I think that is usually a bad idea.
In version 2 you are returning an “unmodifiable” List which is probably a wrapper around the original List. For unmodifiable read “read‑only”. Other code can see what Sales you have, but cannot modify that List. If you change the original List, the view of it available to other code changes too. That may be the most useful return type, but I am not sure.
In version 3 you are creating a new List, which is completely different from the original. Changes in the original and the new List are completely independent and not visible in the other locations.
In version 4 you are using both techniques together. That creates a snapshot List, which has the same state as the original at the time of its creation, but in a read‑only form, so its state doesn't change. It can however be copied into an ordinary List of any kind.
Even if you protect the List from alterations, it may be possible to alter the Sale objects in it.

There are a few other things I am dubious about. Why are you doing a linear search through that List for the address? That suggests to me that a List isn't necessarily the best data structure for your Sales.
You haven't written what you are going to do if the address is the same, but I can see a potential pitfall in line 12 which will catch you out if you aren't careful.
Where does this List<Sale> live? Is it in the GUI? It probably shouldn't be. It sh‍ould probably be in a different class. You shou‍ld have a Sales application which runs independently of the GUI, and the GUI shou‍ld only exist to serve the application.
Minor things: Use i++ rather than i = i + 1 in line 10. Put a space after the semicolon. Why have you called the text searchProp when it appears to mean an address? Call it address or addressSought or similar.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More, I am afraid.
There is something not quite right about extracting the address from a Sale and comparing it. If you are going to compare Sales objects by address, I would have thought the Sale class would have a sameAddress() method or similar.
 
Thomas Molloy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell, thanks for going into more detail about what you mentioned in you previous post, and sorry it has taken me so long to reply. In regards to your concern about the initial list being modified, the purpose of the search button is for the user to enter an address, if the address is found to match an item in the arraylist, the textarea (which I also have in the frame) will display details relevant to the property at that address. No modification of the data is meant to occur.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Molloy wrote:Hi Campbell, thanks
That's a pleasure
. . . No modification of the data is meant to occur.
The operative wor‍d is “meant”. If you have a public setSalesList method, then other code can gain access to the List and modify it. If you complain about that, other developers will say, “You never said we shouldn't modify that List. And why did you make a modifiable List available in the first place?” Or worse. Or they won't let you find out they are up to mischief until something really bad happens.
So return that List in such a fashion that you can't modify the original. You have four options; see which works best for your requirements. Option 1 won't work; maybe option 2 will be better.
 
Thomas Molloy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alrighty. I shall heed your advice. Thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!