Java 使用用户信息对两个对应的数组进行排序
已创建一个Java 使用用户信息对两个对应的数组进行排序,java,arrays,binary,Java,Arrays,Binary,已创建一个jTable,用于存储用户输入的信息ClothesName和price是链接的,因此需要对应两个数组以实现二进制搜索(ClothesName和price)。这一切都是通过一个名为“搜索”的按钮来完成的,但我不太确定如何完成,因此我们将非常感谢您的帮助 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { pr
jTable
,用于存储用户输入的信息ClothesName和price是链接的,因此需要对应两个数组以实现二进制搜索(ClothesName和price)。这一切都是通过一个名为“搜索”的按钮来完成的,但我不太确定如何完成,因此我们将非常感谢您的帮助
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()));
}
如果有一种更简单的方法可以让我从链接的两列中检索数据(clothesname链接到price,因此如果有人搜索一个50英镑的商品,则会出现clothesname),那么我也会非常感激
预期结果是使用collection.sort();-->对衣物名称和价格进行排序(从最低到最高,衣物名称也按顺序排列)只要能实现对方法的二进制搜索。
下面是我向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:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
试一试{
字符串ClothesID=jTextField1.getText();
String closesname=jTextField2.getText();
Catergory=jcombox1.getSelectedItem().toString();
字符串Price=jTextField3.getText();
字符串[]名称={ClothesID,Catergory,ClothesName,Color,Price};
int rowCount=jTable1.getRowCount();
int columnCount=jTable1.getColumnCount();
int nextRow=0;
布尔值emptyRowFlag=false;
字符串s;
做{
s=(字符串)jTable1.getValueAt(nextRow,0);
如果(s!=null和&s.length()!=0)nextRow++;
else emptyRowFlag=true;
}while(nextRow for(int i=0;i据我所知,您需要两个数组,price和name,它们分别按数字和字母顺序排列。您似乎还暗示了它们在某种意义上是链接的。使用两个数组和两个映射最容易做到这一点
实际代码如下所示:
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;
private double[]price;//您使用了double,它是对象包装器,无论哪种方式都有效
//这张地图将服装名称映射到价格
私人地图名称;
//此地图将价格映射到服装名称
私有地图priceToName;
公共名称价格(字符串[]cN,双[]p){
//获取输入并将其设置为私有变量
clothingNames=cN;
价格=p;
//然后我们构造或映射
nameToPrice=newhashmap();
priceToName=newhashmap();
//把我们所有的东西都放到地图上
对于(int i=0;i
最后我们有两个排序数组,clothingNames
和price
,我们有两个映射在这两个值之间切换,nameToPrice
和priceToName
。这意味着您可以搜索名称或价格,并使用映射查找相应的值
您可以使用数组进行搜索,使用二进制搜索查找要查找的值,并使用映射进行转换
此外,您可以使用双向映射,但标准JavaAPI中没有双向映射,因此您必须使用一些外部库,如Apache集合中的BidiMap
最后,我不确定您会使用它到底做什么,因为您可以使用地图从服装中找到价格,反之亦然,除非您特别需要服装名称相对于价格的位置,反之亦然。在我们查看之前,您能发布至少是有效Java代码的代码吗?您正在使用这些数组在某些地方的标量上下文中。例如,this.ClothesName=CloseName
正在尝试将字符串分配给数组。这是不可能的。您是否在考虑类似HashMap的内容?HashMap有键和值,您可以将键设置为特定衣服的名称,而值将是该衣服的价格?@DodgyCodeExce我编辑了原始帖子。@GCP是的!类似这样的东西,但我不知道如何实现二进制搜索。@andersonmohamed你只需要按照算法说的去做:如果你仍然做不到,请告诉我,我会帮助你。
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);
}
}