I have a JTable containing only one columns of , called "Points" Points
I have another JTable containing seven columns of , called "Lines" Lines
I am writing a button handler that removes every point (row) in Points which is not found in Lines as it is not utilized.
I extract the content of Points one point (row) at a time. I extract the entire content of Lines ideally in a String Array. Unluckily the method I use with Lines outputs a Vector. I cannot find a way to convert from Vector to String Array or similar data type. My goal is to search in Lines for the single point from Points without using nested loops. I tried the following instructions but they do not work. Presumably because the method "contains()" does not work when searching a Vector. I would greatly appreciate some suggestion. Thank you in advance.
I tried the following:
btnDeleteUnused.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Vector<Vector> data = linesTableModel.getDataVector();
for(int k = pointsTableModel.getRowCount() - 1; k >= 0; k--)
{
String pn = (String) pointsTableModel.getValueAt(k, 0); // Fetch Point Name at (k_th row, column 0)
System.out.println("Current Point: "+ pn);
System.out.println(data.contains(pn));
}
// TO BE DEVELOPED
}
});
You do indeed have a problem with using
containson yourdataObject. It results because you are attempting to search for, presumably, aString, but each element ofdatais not aString. Each element is anotherVector. Thedatavariable is essentially a 2D Vector. Thus,data.contains(pn)is searching for aStringthat is the same as a row ofVectors, so will always returnfalse.An analogy: Think of
dataas a case. The case contains some number of boxes of candy. But, the lists of types of candy is missing. You want to find whether or not the case contains any caramel candy. The code in the question usingdata.contains(pn)is like going through the case, picking out one-box-at-a-time, and asking "Is this box a piece of caramel?" To find out if the case contains any caramel candy, you want to open each box, and then check each piece in that box.To access a single element of a
Vector<Vector>, you need to use two indexes. See the example in the API.So, to search for a specific
String, you need to search element-by-element with nested loops, or usecontainswithin a loop.The following method can be used to find the first location of an occurrence of a target
String.This uses thecontainsmethod within a loop. It returns anint []with two elements: One for the outer index, the other for the inner index:Following is a refactored
indexOf (Vector<Vector> theVector, String target)method. Instead of usingcontains, the code shows how each element might be accessed. The code uses some intermediate variables, which should make it easier to follow.It also fixes another issue, which is explained after the code sample:
The other issues is this:
An element of
JTabledoes not necessarily have to be aString. So, it isn't necessarily the case that each element ofdatais aString. An element ofdatamight be, hypothetically, aFoo. Suppose your code is looking for an element ofdatathat corresponds to"Eleven". It so happens, theElevenis actually associated with theFoo, because itstoString()method returns"Eleven". The first version won't work. The refactored version solves this by usingString#equalsmethod.Note that the refactored version actually uses
.equalsIgnoreCaseto illustrate the revision makes that possible.Your code has
System.out.println(data.contains(pn));.You could use the above method by changing that line to
System.out.println(indexOf (data, pn)[0] != -1);When you develop more code, you could access the specific element like this: