已创建jTable
来存储用户输入的信息。 ClothesName 和价格是链接的,因此需要对应两个数组以实现二进制搜索(衣服名称和价格)。这一切都在一个名为“搜索”的按钮中完成,但是我不太清楚如何做到这一点,所以任何帮助都会受到赞赏。
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
private String[] ClothesName;
private Double[] price;
int iPrice = Integer.parseInt(price);
public findingPrice(String ClothesName, int iPrice) {
this.ClothesName = ClothesName;
this.iPrice = iPrice;
}
public String getClothesName() {
return this.ClothesName;
}
public double iPrice() {
return this.iPrice;
}
@Override
public int compareTo(findingPrice price)
{
return iPrice.compareTo(findingPrice.getiPrice()));
}
如果有一种更简单的方法让我从链接的两列中检索数据(衣服名称与价格挂钩,那么如果有人搜索£50 的项目,则衣服名称会出现)那也是值得赞赏的。
预期的结果是使用collection.sort(); --> as long as binary search can be implemented to the method.
这是我向jTable1添加信息的方法。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try{
String ClothesID = jTextField1.getText();
String ClothesName = jTextField2.getText();
Catergory = jComboBox1.getSelectedItem().toString();
String Price = jTextField3.getText();
String[] name = {ClothesID, Catergory, ClothesName, Colour, Price};
int rowCount = jTable1.getRowCount();
int columnCount = jTable1.getColumnCount();
int nextRow = 0;
boolean emptyRowFlag = false;
String s;
do {
s = (String)jTable1.getValueAt(nextRow,0);
if (s != null && s.length() != 0) nextRow++;
else emptyRowFlag = true;
} while (nextRow < rowCount && !emptyRowFlag);
for (int i=0; i<columnCount; i++)
jTable1.setValueAt(name[i],nextRow,i);
} catch (Exception e){ JOptionPane.showMessageDialog(null, "Error"); }
jTextField1.setText("");
jTextField2.setText("");
jTextField2.setText("");
jTextField3.setText("");
// TODO add your handling code here:
}
这是排序方法,感谢@Dax Loy:
public class NamePrice {
private String[] clothingNames;
private double[] price; //You used Double which is the object wrapper, either way works
//This map maps the clothing name to price
private Map<String, double> nameToPrice;
//This map maps the price to the clothing name
private Map<double, String> priceToName;
public NamePrice(String[] cN, double[] p){
//Takes the input and set it to the private variables
clothingNames = cN;
price = p;
//Then we construct or map
nameToPrice = new HashMap<String, double>();
priceToName = new HashMap<double, String>();
//Put everthing we have into the maps
for(int i = 0; i < clothingNames.length && i < price.length; i++){
nameToPrice.put(clothingNames[i], price[i]);
priceToName.put(price[i], clothingNames[i]);
}
//Then we sort the arrays
Arrays.sort(clothingNames);
Arrays.sort(price);
}
但我已经有一个公共课:public class CSInfo extends javax.swing.JFrame
我现在不知道如何实现二进制搜索。
答案 0 :(得分:1)
根据我的理解,您需要两个数组,价格和名称,分别按数字和字母顺序排序。你似乎也暗示了它们在某种意义上被联系起来的想法。使用两个数组和两个地图最容易做到这一点。
实际的代码会是这样的:
public class NamePrice {
private String[] clothingNames;
private double[] price; //You used Double which is the object wrapper, either way works
//This map maps the clothing name to price
private Map<String, double> nameToPrice;
//This map maps the price to the clothing name
private Map<double, String> priceToName;
public NamePrice(String[] cN, double[] p){
//Takes the input and set it to the private variables
clothingNames = cN;
price = p;
//Then we construct or map
nameToPrice = new HashMap<String, double>();
priceToName = new HashMap<double, String>();
//Put everthing we have into the maps
for(int i = 0; i < clothingNames.length && i < price.length; i++){
nameToPrice.put(clothingNames[i], price[i]);
priceToName.put(price[i], clothingNames[i]);
}
//Then we sort the arrays
Arrays.sort(clothingNames);
Arrays.sort(price);
}
}
最终我们有两个排序的数组clothingNames
和price
,我们有两个地图可以在两个值nameToPrice
和priceToName
之间切换。这意味着您可以搜索名称或价格,并使用地图查找相应的值。
您可以使用数组搜索二进制搜索来查找您要查找的值以及要转换它们的地图。
此外,您可以使用双向映射,但标准Java API中没有一个,因此您必须使用一些外部库,如Apache Collections中的BidiMap。
最后,我不确定你究竟会使用这个,因为你可以使用地图来找到衣服的价格,反之亦然,除非你特别需要衣服名称相对于价格的位置,反之亦然