jTable
은 사용자가 입력 한 정보를 저장하기 위해 생성되었습니다. 의류 이름 및 가격이이므로 이진 검색을 구현하려면 두 개의 배열이 일치해야합니다 (clothesName & 가격). 이 버튼은 모두 'Search'이라고 불리는 버튼으로 이루어집니다. 그러나이 방법을 잘 모르겠으니 도움을 받으실 수 있습니다.사용자 정보가있는 두 개의 해당 배열 정렬
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 clothesname가 온다 인 항목을 검색합니다 그래서 만약 clothesname 가격에 링크)를 연결하는 두 개의 열에서 데이터를 검색하기위한 쉬운 방법이 있다면 또한 그것은 또한 평가 될 것입니다.
예상되는 결과는 clothesName을 가지고 있으며 가격은 여기 내 jTable1에 정보를 추가하는 나의 방법이다 collection.sort(); --> as long as binary search can be implemented to the method.
를 사용 (위해 낮은 또한 clothesname에 가장 높은에 가격) 분류.
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 로이 덕분에 정렬하는 방법입니다 :
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
을 지금 이걸로.
살펴보기 전에 적어도 유효한 Java 코드 인 코드를 게시 할 수 있습니까? 이러한 배열은 일부 장소의 스칼라 컨텍스트에서 사용하고 있습니다. 예를 들어'this.ClothesName = CloseName'은 문자열을 배열에 할당하려고합니다. 그건 불가능하다. – DodgyCodeException
HashMap과 같은 것을 생각하고 있습니까? HashMap에는 키와 값이 있습니다. 특정 의류의 이름으로 키를 설정할 수 있으며 값은 해당 의류의 가격입니까? https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html –
prsvr
@DodgyCodeException 원본 게시물을 편집했습니다. –