Re: Determine index from array reference?
Patricia Shanahan wrote:
Knute Johnson wrote:
Patricia Shanahan wrote:
I'm still very unclear about what the actual accesses are. Why do you
want the index? Presumably, there is some bigger operation involved that
uses the index.
Generally, if you find yourself thinking in terms of "you can't get to
the key with the value" it means you have the map the wrong way round.
However, without knowing what the index is for, it is impossible to say.
Patricia
Patricia:
I'm sorry you must have missed my reply to your earlier message. 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.
Now I'm really confused: "You guys are obviously not listening to this
neanderthal programmer. I only have one list"
1. The array of JTextField references.
2. The "another array" in which you are going to set some values.
I decided I had misunderstood what was going on, and had no idea what
the index was used for, after reading the comment about "only one list".
Why not have a map, indexed by JTextField, with the object you need to
update on actionPerformed as value?
Patricia
Patricia:
Sorry for the confusion. I have an array of objects. I have an array
of JTextFields to collect data to update elements of the first object
array. Both arrays need to be accessed by their index in other code.
In the ActionListener I can easily get a reference to the JTextField.
What I was looking for was simpler way of finding the index into the
JTextField array.
After your question I think I see what Lew meant by two lists. What I
thought he meant was my indexes.
In any case, my method is orders of magnitude faster than an ArrayList.
The tests below require 400mb of heap. Just make the array smaller if
you don't have that much.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class test4 {
public static void main(String[] args) {
final int LEN = 100000;
ArrayList<JTextField> array = new ArrayList<JTextField>(LEN);
JTextField[] tf = new JTextField[LEN];
long start = 0;
for (int i=0; i<LEN; i++) {
tf[i] = new JTextField("",4);
array.add(tf[i]);
}
System.out.println("Start");
start = System.currentTimeMillis();
for (int i=0; i<LEN; i++) {
int index = array.indexOf(tf[i]);
}
System.out.println(System.currentTimeMillis() - start);
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class test5 {
public static void main(String[] args) {
final int LEN = 100000;
long start = 0;
class ITextField extends JTextField {
int n;
public ITextField(String text, int columns, int index) {
super(text,columns);
n = index;
}
public int getIndex() {
return n;
}
}
ITextField[] tf = new ITextField[LEN];
for (int i=0; i<LEN; i++) {
tf[i] = new ITextField("",4,i);
}
System.out.println("Start");
start = System.currentTimeMillis();
for (int i=0; i<LEN; i++) {
int index = tf[i].getIndex();
}
System.out.println(System.currentTimeMillis() - start);
}
}
--
Knute Johnson
email s/nospam/knute/