2017-11-08 18 views
0

누구나 사용자 입력을 인쇄하고 ArrayList를 같은 대화 상자에서 정렬 할 수 있습니까? (예 : User input = **Toyota, Mazda, Ford**. Sorted input =**Ford, Mazda, Toyota**).똑바로 입력을 표시하고 같은 대화 상자에서 ArrayList를 정렬하는 방법

대화 상자로 작업하는 것은 이번이 처음입니다. 사용할 수있는 추가 도구가 있습니까? 또는 Collections.sort(auto);을 별도의 클래스로 작성한 다음 인쇄 할 때 삽입해야합니까? 여기

코드가 모습입니다 같은 :

Scanner in = new Scanner(System.in); 
    ArrayList<String> auto = new ArrayList<>(); 
    boolean done = false; 
    do { 
     String autoList = JOptionPane.showInputDialog(null, "Enter an Auto you like (q to finish):", 
       "click OK for each auto entry"); 
     if (autoList.equalsIgnoreCase("q")) { 
      break; 
     } 
     auto.add(autoList); 
    } while (!done); 
    Collections.sort(auto); 
    JOptionPane.showMessageDialog(null, "Arra list " + auto 
      + "\n Sorted list is " + auto); 
    // should print straight user's input on one line and sorted Array list on the second line 
    in.close(); 
+0

당신이 원래의 목록을 인쇄되지 않습니다하는 문제를 건가요? 원래 목록이 수정되었다고 JavaDoc에 명시되어 있기 때문에 이것이 문제가된다. 'JOptionPane.showMessageDialog (null, "Arra list"+ auto + "\ n 정렬리스트는"+ Collections.sort (auto) ");와 같이 정렬 할 수있다. –

+0

예, 원본 목록이 인쇄되지 않았고 대화 상자가'Collections.sort (자동) '바로 뒤에 오르기 때문에 원본 목록이 인쇄되어 있다는 것을 알고 있습니다. 'Collections.sort (auto)'를 추가하려고 시도했지만 _ "연산자 +가 인수 유형 String, void"에 대해 정의되지 않았습니다. "+"연산자를 "+"로 변경하면 오류 메시지에 _ "JOptionPane 유형의 메서드 showMessageDialog (Component, Object)가 인수 (null, String, void)에 적용 할 수 없습니다"_ : – Rish

답변

1

문제는 당신이 Collections.sort(list)를 호출 할 때,리스트가 정렬 될 것입니다, 그래서 당신은 메시지 상자에 두 번 auto을 인쇄 할 때, 그것은 인쇄입니다 소트 된리스트를 2 회 더우기 반환 유형은 void이므로이 값을 문자열의 끝에 연결할 수 없습니다. 여기에 예제가 있습니다.

Scanner in = new Scanner(System.in); 
    ArrayList<String> auto = new ArrayList<>(); 
    boolean done = false; 
    do { 
     String autoList = JOptionPane.showInputDialog(null, "Enter an Auto you like (q to finish):", 
       "click OK for each auto entry"); 
     if (autoList.equalsIgnoreCase("q")) { 
      break; 
     } 
     auto.add(autoList); 
    } while (!done); 

    System.out.println("auto: "+ auto); 

    Collections.sort(auto); 

    System.out.println("auto: "+ auto); 

    JOptionPane.showMessageDialog(null, "Arra list " + auto 
      + "\n Sorted list is " + auto); 
    // should print straight user's input on one line and sorted Array list on the second line 
    in.close(); 

출력 :

auto: [Ford, Mazda, Toyota, BMW] 
auto: [BMW, Ford, Mazda, Toyota] 

메시지 상자에 인쇄하고, 목록이 이미 정렬됩니다. 한 가지 옵션은 정렬 전에 정렬되지 않은 목록의 복사본을 만드는 것이므로 정렬 후에 정렬되지 않은 값에 대한 참조가 있습니다. 여기

당신이 할 거라고 방법의 예입니다

Scanner in = new Scanner(System.in); 
    ArrayList<String> auto = new ArrayList<>(); 
    ArrayList<String> unsortedAutos = new ArrayList<>(); 
    boolean done = false; 
    do { 
     String autoList = JOptionPane.showInputDialog(null, "Enter an Auto you like (q to finish):", 
       "click OK for each auto entry"); 
     if (autoList.equalsIgnoreCase("q")) { 
      break; 
     } 
     auto.add(autoList); 
    } while (!done); 


    unsortedAutos = (ArrayList<String>) auto.clone(); 
    Collections.sort(auto); 

    System.out.println("auto: "+ unsortedAutos); 
    System.out.println("auto: "+ auto); 

    JOptionPane.showMessageDialog(null, "Arra list " + unsortedAutos 
      + "\n Sorted list is " + auto); 
    // should print straight user's input on one line and sorted Array list on the second line 
    in.close(); 

출력 :

auto: [Mazda, Ford, BMW] 
auto: [BMW, Ford, Mazda] 
+0

이것은 내가 필요한 것입니다. 그런 훌륭한 제안! 나는 ArrayList 에 대해 조금 더 연구 할 것이며, 내가 더 할 수있는 것이 무엇인지 살펴볼 것이다. 정말 감사합니다. – Rish