2012-12-09 142 views
-1

可能重複:
Best way to sort 2 array lists?Java的獲取用戶輸入的第一個名字,姓氏,年齡,排序按字母順序,然後按年齡

package bubblesort; 

import java.util.*; 

public class BubbleSort { 

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    Scanner input2 = new Scanner(System.in); 
    List<String> lastnamesList = new ArrayList(); 
    List<String> firstnamesList = new ArrayList(); 
    List<Integer> ageList = new ArrayList();  
    List<String> fullnamesList = new ArrayList(); 
    int decider; 
    String EOF = "EOF"; 


    System.out.println("Enter in 0 to sort by Last Name, Enter in 1 to sort by Age"); 
    decider = input.nextInt(); 

    /*if(decider == 0) 
    { 
    System.out.println("Sorting by Last Name"); 
     for(int i=0;i<3;i++) 
     { 
     System.out.println("Please Enter in your Last Name, FirstName, Age: "); 
     fullnamesList.add(input2.nextLine()); 
     } 
    Collections.sort(fullnamesList); 
    System.out.println("Sorting by Last Name: "); 
    System.out.println(fullnamesList); 
    }*/ 
    //String[] lastnamesArray = lastnamesList.toArray(new String[lastnamesList.size()]); 
    //String[] firstnamesArray = firstnamesList.toArray(new String[firstnamesList.size()]); 
    if(decider == 1) 
    { 
    System.out.println("Sorting by Age"); 
     for(int i=0;i<3;i++) 
     { 
     System.out.println("Please Enter in your Last Name: "); 
     lastnamesList.add(input2.nextLine()); 
     System.out.println("Please Enter in your First Name: "); 
     firstnamesList.add(input2.nextLine()); 
     System.out.println("Please Enter in your Age: "); 
     ageList.add(input.nextInt()); 

     } 

    String[] firstnamesArray = firstnamesList.toArray(new String[firstnamesList.size()]); 
    String[] lastnamesArray = lastnamesList.toArray(new String[lastnamesList.size()]); 
    fullnamesList.addAll(firstnamesList); 
    fullnamesList.addAll(lastnamesList); 
    String[] fullnamesArray = fullnamesList.toArray(new String[firstnamesList.size()+lastnamesList.size()]); 

    Collections.sort(fullnamesList); 
    System.out.println("Sorting by Age"); 
    System.out.println(fullnamesList); 
    } 
} 
public static void sortStringBubble(String x[]) 
{ 
    int j; 
    boolean flag = true; 
    String temp; 

    while(flag) 
    { 
     flag = false; 
     for(j = 0;j < x.length -1 ; j++) 
     { 
      if (x [j].compareToIgnoreCase(x[j+1])>0) 
      { 
       temp = x[j]; 
       x[j] = x[j+1]; 
       x[j+1] = temp; 
       flag = true; 
      } 
     } 
    } 
} 

}

IM試圖保持名字,姓氏和年齡在一起,但我希望它按字母順序排序,而不是排序m分開。

+0

不重複。答案顯示OP的方法是錯誤的,只需要一個ArrayList。 – fredt

回答

2

而不是

List<String> lastnamesList = new ArrayList(); 
List<String> firstnamesList = new ArrayList(); 
List<Integer> ageList = new ArrayList(); 

使用

List<Person> personList = new ArrayList<>(); 

其中Person是你自己寫的類。比只有一個列表要排序。

Person將有字段lastNamefirstNameage,所以他們將永遠在一起。

此外,請參閱thisthis後,或一些these

+0

我對java相當陌生,所以person類對我來說很困惑,有沒有任何算法可以根據字母順序交換姓名和年齡的位置,並將這些字段保存在一起 –

+0

沒有合理的替代方法來替代' Person類,因爲沒有根據一個列表的索引變化對多個列表進行排序的算法。 – jlordo

+0

您需要Person類的比較器。 – fredt

相關問題