Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
I've got an array of JTextFields. These JTextFields have ActionListeners
attached. In the actionPerformed() I need to get the index of the
JTextField that fired the action to set some values in another array.
We're saying not to do that. Instead of having an array of JTextFields and an
array of values in another array, have a map of JTextField to otherValue.
This replaces BOTH arrays. If you need to preserve order (and you probably
do), a LinkedHashMap is perfect.
In other parts of my code I also need to access the JTextField[] by
index.
It all depends, of course, on what your overall app is, but it seems quite
believeable that these indexed uses of the JTextField[] could be replaced by
faster clearer use of a different data structure.
So the dreaded Vector/ArrayList suggestions were good ones
because they did solve the problem. Then somehow we got onto the
multiple list/Map tangent which I don't really have and doesn't work
because of the 'you can't get the key from the value' problem.
Yet another requirement. Which likely leads to writing a real data object
that contains both directions of mapping if you need. Then you can be free to
change the implementation to one that fits best.
Instead of two arrays, or an array and an ArrayList, have a TextFieldInfo
class, with an addField(JTextField, OtherThing), a getFoo(JTextField), and a
getTextField(OtherThing), and any other mechanisms you need. Inside that,
keep two hashes, one for each direction. Or some other mechanism if that
makes more sense.
Chris suggested just looping through the array. I asked but haven't gotten
an answer if he thought that was still a good solution for very large
arrays. In my case however the array only has 16 elements.
Looping through the array is exactly the same work done by the indexOf()
method of most List implementations.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
I already have. I'm going to stick with the neanderthal approach.