2017-12-04 1 views
0

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

을 지금 이걸로.

+0

살펴보기 전에 적어도 유효한 Java 코드 인 코드를 게시 할 수 있습니까? 이러한 배열은 일부 장소의 스칼라 컨텍스트에서 사용하고 있습니다. 예를 들어'this.ClothesName = CloseName'은 문자열을 배열에 할당하려고합니다. 그건 불가능하다. – DodgyCodeException

+1

HashMap 과 같은 것을 생각하고 있습니까? HashMap에는 키와 값이 있습니다. 특정 의류의 이름으로 키를 설정할 수 있으며 값은 해당 의류의 가격입니까? https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html – prsvr

+0

@DodgyCodeException 원본 게시물을 편집했습니다. –

답변

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

    } 



} 

이것은 우리가 두 개의 정렬 된 배열, clothingNamesprice을 갖는 끝을, 우리는 두 개의 값, nameToPricepriceToName 사이를 전환하는 방법은 두지도가 있습니다. 즉, 이름이나 가격을 검색하고지도를 사용하여 해당 값을 찾을 수 있습니다.

배열을 사용하면 이진 검색을 사용하여 찾고있는 값과이를 변환 할 맵을 찾을 수 있습니다.

또한 양방향 맵을 사용할 수 있지만 표준 Java API에는 하나도 없으므로 Apache Collections의 BidiMap과 같은 외부 라이브러리를 사용해야합니다.

마지막으로, 가격 또는 부도와 관련하여 의류 이름의 위치가 구체적으로 필요하지 않으면지도를 사용하여 의류에서 가격을 찾거나 그 반대로지도를 사용할 수있는 것처럼 정확히 무엇을 사용해야할지 잘 모르겠습니다. 그 반대.