2016-11-26 61 views
1

對於例如我有一個數組:在列表排序的ArrayList由指定的值

String[] Array1 = 
     {"15", "1", "D1", "Wine",  "1", "0", 
     "15", "3", "D3", "Tap water", "2", "2", 
     "15", "1", "M1", "Fish",  "3", "0", 
     "9", "5", "D4", "Coffee",  "2", "2", 
     "9", "2", "P2", "Cake",  "2", "1" 
     }; 
someList.addAll(Arrays.asList(Array1)); 

我想要根據在每一行,即1,3-第二值以數字順序排序有些類似於這種類型的ArrayList的,1,5,2代入1,1,2,3,5,同時保持另一個變量不變。我不能讓另一個類來存儲這些變量。有人知道我可以如何對它們進行排序嗎?

+0

嗨!你試過什麼了?你可以把這個問題呢? –

+3

看起來你正在使用錯誤的數據結構 –

+0

你的「行」在你的數據結構中沒有被標記爲這樣 - 把它變成一個行數組(首先有一些「行」類型,例如數組)會使這個變得更簡單。那麼你只需要實現一個比較器,並且可以使用標準的排序功能。 –

回答

0

如果你創建了一些有用的方法和比較,你可以使用一個經典的排序方法像冒泡排序:

public static void main(String[] args) {  
    String[] array1 = 
     {"15", "1", "D1", "Wine",  "1", "0", 
     "15", "3", "D3", "Tap water", "2", "2", 
     "15", "1", "M1", "Fish",  "3", "0", 
     "9", "5", "D4", "Coffee",  "2", "2", 
     "9", "2", "P2", "Cake",  "2", "1" 
     }; 
    Comparator<String[]> comparator = new Comparator<String[]>(){ 
     @Override 
     public int compare(String[]a1, String[] a2) { 
      return Integer.valueOf(a1[1]).compareTo(Integer.valueOf(a2[1])); 
     } 
    }; 
    int lineLength=6; 
    bubbleSort(array1,lineLength,comparator); 
    System.out.println(Arrays.toString(array1)); 
} 
//classic bubble-sort algorithm 
public static void bubbleSort(String[]array1,int lineLength,Comparator<String[]> comparator){ 
    int numRow=array1.length/lineLength; 
    for(int i=0;i<numRow;i++){ 
     for(int j=i+1;j<numRow;j++){ 
      String[] extractArrayI = extractArray(array1, i, lineLength); 
      String[] extractArrayJ = extractArray(array1, j, lineLength); 
      if(comparator.compare(extractArrayI, extractArrayJ)>0){ 
       swichLines(array1,i,j,lineLength); 
      } 
     } 
    } 
} 
//extract i-th row 
public static String[] extractArray(String[]array,int i, int lineLength){ 
    String [] a= new String[lineLength]; 
    System.arraycopy(array, i*lineLength, a, 0, lineLength); 
    return a; 
} 
//Switch line i,j 
public static void swichLines(String[]array,int i, int j,int lineLength){ 
    String [] temp = new String[lineLength]; 
    System.arraycopy(array, i*lineLength, temp, 0, lineLength); 
    System.arraycopy(array, j*lineLength, array, i*lineLength, lineLength); 
    System.arraycopy(temp, 0, array, j*lineLength, lineLength); 
} 

更新:使用List<String>代替String[]

public static void main(String[] args) { 
    String[] array1 = 
      {"15", "1", "D1", "Wine",  "1", "0", 
      "15", "3", "D3", "Tap water", "2", "2", 
      "15", "1", "M1", "Fish",  "3", "0", 
      "9", "5", "D4", "Coffee",  "2", "2", 
      "9", "2", "P2", "Cake",  "2", "1" 
      }; 
    List<String> list = Arrays.asList(array1); 
    Comparator<List<String>> comparator = new Comparator<List<String>>(){ 
     @Override 
     public int compare(List<String>a1, List<String> a2) { 
      return Integer.valueOf(a1.get(1)).compareTo(Integer.valueOf(a2.get(1))); 
     } 
    }; 
    int lineLength=6; 
    System.out.println(list.toString()); 
    bubbleSort(list,lineLength,comparator); 
    System.out.println(list.toString()); 
} 
//classic bubble-sort algorithm 
public static void bubbleSort(List<String> list,int lineLength,Comparator<List<String>> comparator){ 
    int numRow=list.size()/lineLength; 
    for(int i=0;i<numRow;i++){ 
     for(int j=i+1;j<numRow;j++){ 
      List<String> extractArrayI = extractArray(list, i, lineLength); 
      List<String> extractArrayJ = extractArray(list, j, lineLength); 
      if(comparator.compare(extractArrayI, extractArrayJ)>0){ 
       swichLines(list,i,j,lineLength); 
      } 
     } 
    } 
} 
//extract i-th row 
public static List<String> extractArray(List<String> list,int i, int lineLength){ 
    return list.subList(i*lineLength, i*lineLength+lineLength); 
} 
//Switch line i,j 
public static void swichLines(List<String>list,int i, int j,int lineLength){ 
    List<String>tempI = new ArrayList<String>(list.subList(i*lineLength, i*lineLength+lineLength)); 
    List<String>tempJ = new ArrayList<String>(list.subList(j*lineLength, j*lineLength+lineLength)); 
    replaceSublist(list,tempJ,i,lineLength); 
    replaceSublist(list,tempI,j,lineLength); 
} 
//replace sublist 
private static void replaceSublist(List<String> list, List<String> temp, int line, int lineLength) { 
    for (int k=0; k<lineLength; k++) 
    { 
     list.set(line*lineLength+k, temp.get(k)); 
    } 
} 
+0

這很好,但我喜歡,我將它存儲在一個arrayList(我必須),所以我如何排序arrayList呢? –

+0

我用列表解決方案更新了答案;) – user6904265

0

您使用了錯誤的數據結構。數組用於包含相同類型信息的相同類型的多個變量

我建議你創建一個類,並創建包含該類的對象數組,像這樣:

Drink.class

class Drink{ 
    private int a; 
    private int b; 
    private String c; 
    private String drinkName; 
    private int d; 
    private int e; 

    public Drink(int a,int b,String c,String drinkName,int d,int e){ 
     this.a=a; 
     this.b=b; 
     this.c=c; 
     this.drinkName=drinkName; 
     this.d=d; 
     this.e=e; 
    } 

     // Getters and setters here 
     public String getDrinkName(){ 
      return drinkName; 
     } 
     // ..... 
} 

,然後在主類:

class MainClass{ 
    List<Drink> drinks; 
    public static void main(String[] args){ 
     drinks = new ArrayList<>(); 
     drinks.add(new Drink(15,1,"D1","Wine",1,0)); 
     drinks.add(new Drink(15,3,"D3","Tap Water",2,2)); 
     // Etc... 
     // You can retrieve elements using .get(index) and then use getters on it to retrieve informations 
     System.out.println(drinks.get(0).getDrinkName()); 
     Collections.sort(drinks,new Comparator<Drink>(){ 
      @Override 
      public int compare(Drink d1, Drink d2){ 
       // Edit this method as you need 
       return d1.getA().compareTo(d2.getA()); 
      } 

     }); 
    } 
} 
+0

這是一種不好的使用方式,但我必須爲我的任務 –

0

如果將所有元素作爲String值存儲到0中,您將無法輕鬆地對元素進行排序。相反,您可以使用OOP並定義名爲MyCustomData的自定義類型(類),然後將數據作爲對象加載。

所以,你需要按照下面的步驟:

(1)定義自定義類MyCustomData

(2)創建對象爲MyCustomData並將它們加載到陣列。

(3)現在,排序使用Comparator

您可以參考下面的代碼註釋的數組:

MyCustomData類(命名該類正常):

public class MyCustomData { 
     private int value1;//holds your element to be sorted 
     //other values //define other values to hold fish, etc.. 

     public int getValue1() { 
      return value1; 
     } 

     public void setValue1(int value1) { 
      this.value1 = value1; 
     } 
    } 

排序MyCustomData數組:

public static void main(String[] args) { 

     MyCustomData[] myCustomDataArray = new MyCustomData[5]; 
     MyCustomData myCustomData1 = new MyCustomData(); 
     myCustomData1.setValue1(1); 
     myCustomDataArray[0] = myCustomData1; 
     //Create and Load other objects myCustomDataArray[1] , [2], ....into array 

     Comparator<MyCustomData> comp = (MyCustomData data1, MyCustomData data2) 
          -> data1.getValue1()-data2.getValue1(); 
     Arrays.stream(myCustomDataArray).sorted(comp); 
} 
+0

Im實際上不應該使用我的自定義類來傷心地排序它。 –