簡體   English   中英

排序2D數組,但將列元素保持在一起

[英]Sorting 2D Array but keeping column elements together

我有一個2行2列n列的二維數組。

我正在嘗試使用內置的Arrays.sort方法對數組進行排序,但是我在與比較器進行斗爭。 我想按第一行對2D數組進行排序,以便第二行元素將與原始元素保持對齊,即。 保持列在一起。

例如原始數組:

int[] unsortedArray = new int[][]{ { 6, 3, 1, 2, 3, 0}, { 2, 1, 6, 6, 2, 4},

排序數組:

int[] unsortedArray = new int[][]{ { 0, 1, 2, 3, 3, 6}, { 4, 6, 6, 1, 2, 2},

這樣做的最佳方法是什么? 干杯。

請嘗試以下操作:

免責聲明:未測試:)

myarray = new int[10][2];

// populate the array here

Arrays.sort(myarray, new Comparator<int[]>() {
    public int compare(int[] a, int[] b) {
        if (a[0] > b[0])
            return 1;
        else if (a[0] < b[0])
            return -1;
        else {
            return 0;
        }
    }
};

您將必須編寫一個自定義排序。 研究可比比較器

類似於以下內容:

public class MyComparator implements Comparator<T[]> {
    int columnToSortOn;
    public MyComparator(int columnToSortOn) {
        this.columnToSortOn = columnToSortOn;
    }
    @Override
    public int compare(T[] array1, T[] array2) {
        return array1[columnToSortOn].compareTo(array2[columnToSortOn]);
    }
}

這是保持列對齊的另一種方法。 這使用一個簡單的類來保存兩個列元素。

class TwoInts  {
   public final int aElement;
   public final int bElement;
   public TwoInts(int a_element, int b_element)  {
      aElement = a_element;
      bElement = b_element;
   }
}

每一列(分別來自子數組1和2)一一放置在此對象中,並立即放入Map<Integer,List<TwoInts>> 關鍵是從元件intArrayArray[0]並且該值是列表 TwoInts ,因為有可能(和在您的示例代碼,都是)中重復的值intArrayArray[0]

然后,您遍歷地圖的鍵集,並將其替換為數組。

完整代碼:

  import  java.util.ArrayList;
  import  java.util.Arrays;
  import  java.util.Iterator;
  import  java.util.List;
  import  java.util.Map;
  import  java.util.TreeMap;
/**
   <P>{@code java SortOneArrayKeepSecondArrayElementsAligned}</P>
 **/
public class SortOneArrayKeepSecondArrayElementsAligned  {
   public static final void main(String[] ignored)  {
      int[][] intArrayArray = new int[][]{
        { 6, 3, 1, 2, 3, 0},
        { 2, 1, 6, 6, 2, 4}};
      output2DArray("Unsorted", intArrayArray);

      Map<Integer,List<TwoInts>> twoIntMap = new TreeMap<Integer,List<TwoInts>>();
      for(int i = 0; i < intArrayArray[0].length; i++)  {
         int intIn0 = intArrayArray[0][i];
         if(!twoIntMap.containsKey(intIn0))  {
            List<TwoInts> twoIntList = new ArrayList<TwoInts>(intArrayArray.length);
            twoIntList.add(new TwoInts(intArrayArray[0][i], intArrayArray[1][i]));
               twoIntMap.put(intIn0, twoIntList);
         }  else  {
            twoIntMap.get(intIn0).add(new TwoInts(intArrayArray[0][i], intArrayArray[1][i]));
         }
      }

      int idx = 0;
      Iterator<Integer> itr2i = twoIntMap.keySet().iterator();
      while(itr2i.hasNext())  {
         List<TwoInts> twoIntList = twoIntMap.get(itr2i.next());
         for(TwoInts twoi : twoIntList)  {
            intArrayArray[0][idx] = twoi.aElement;
            intArrayArray[1][idx++] = twoi.bElement;
         }
      }

      output2DArray("Sorted", intArrayArray);
   }
   private static final void output2DArray(String description, int[][] twoD_array)  {
      System.out.println(description + ":");
      System.out.println("0: " + Arrays.toString(twoD_array[0]));
      System.out.println("1: " + Arrays.toString(twoD_array[1]));
      System.out.println();
   }

}
class TwoInts  {
   public final int aElement;
   public final int bElement;
   public TwoInts(int a_element, int b_element)  {
      aElement = a_element;
      bElement = b_element;
   }
}

輸出:

[C:\java_code\]java SortOneArrayKeepSecondArrayElementsAligned
Unsorted:
0: [6, 3, 1, 2, 3, 0]
1: [2, 1, 6, 6, 2, 4]

Sorted:
0: [0, 1, 2, 3, 3, 6]
1: [4, 6, 6, 1, 2, 2]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM