简体   繁体   中英

sorting two corresponding arrays with user information

A jTable has been created to store information the user inputs. ClothesName and price is linked therefore two arrays need to be corresponding in order to implement binary search, (the clothesName & price). This is all done in a button called ' Search ' however I'm not quite sure on how to do this so any help would be appreciated.

  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()));
}

If there is an easier way for me to retrieve data from the two columns that are linked (clothesname is linked to price so if someone searches for an item that is £50 the clothesname comes up) then also that would be appreciated also.

The expected result is to have the clothesName and Price sorted (price lowest to highest with the clothesname also in order) using collection.sort(); --> as long as binary search can be implemented to the method. collection.sort(); --> as long as binary search can be implemented to the method.

Here is my method to add information to my 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:
    }

And this is the method to sort thanks to the @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);

   }

But i already have a public class: public class CSInfo extends javax.swing.JFrame

I have no idea how to implement binary search into this now.

From what I understand you are wanting two arrays, price and name, that are ordered numerically and alphabetically respectively. You also seem to hint at the idea of them being linked in some sense. This would be most easily done using two arrays and two maps.

The actual code would go something like this:

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);

   }



}

This ends up with us having two sorted arrays, clothingNames and price , and we have two maps to switch between the two values, nameToPrice and priceToName . This means you can search through either name or price and use the maps to find the corresponding value.

You would use the Arrays to search using your binary search to find the value you are looking for and the maps to convert them.

Also, you could use a bidirectional map but there is not one in the standard Java API so you would have to use some external library like the BidiMap from Apache Collections.

Finally, I am not sure what exactly you would use this for as you can just use the map to find the price from clothing or vice versa unless you specifically need the position of the name of clothing with respect to price or vice versa.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM