2016-11-26 59 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实际上不应该使用我的自定义类来伤心地排序它。 –