简体   繁体   English

通过比较器对Java数组进行排序

[英]Java Array sort by means of a comparator

The complete code can be found at: https://skydrive.live.com/redir?resid=7B7D2F11B13EF9C9!54468&authkey=!AD4fD8sGgc7oJIE 完整的代码可以在以下位置找到: https : //skydrive.live.com/redir?resid=7B7D2F11B13EF9C9!54468&a​​uthkey=!AD4fD8sGgc7oJIE

This is part of the code: 这是代码的一部分:

g_sorted = 0;
for (int l_loop = 0; l_loop < l_length; l_loop++)
{
    if (!l_IntegerArray[l_loop].equals(g_exclude))
    {
        g_tag[g_sorted] = l_loop;
        g_tosort_Integer[g_sorted] = l_IntegerArray[l_loop];
        g_sorted++;
    }
} // for (int l_loop = 0; l_loop < p_toSort; l_loop++)

Arrays.sort
(g_tag, 0, g_sorted, new Comparator<Integer>()
    {
        public int compare(Integer i1, Integer i2)
        {
            return Integer.compare(g_tosort_Integer[i1], g_tosort_Integer[i2]);
        }
    }
);

g_tosort_Integer, g_tag, g_tosort_Integer are 'Integer'. g_tosort_Integer,g_tag,g_tosort_Integer是“整数”。 g_exclude is used to exclude items which must not be part of the sort. g_exclude用于排除不能属于排序的项目。

When no items are excluded (no item has the value equal to g_exclude or the if-statement is commented), everything works fine. 当没有项目被排除时(没有项目的值等于g_exclude或if语句被注释),一切正常。

When 1 or more items are excluded I get a NullPointerException: 当排除1个或多个项目时,我得到NullPointerException:

Exception in thread "main" java.lang.NullPointerException
at TestSort$1.compare(TestSort.java:53)
at TestSort$1.compare(TestSort.java:50)
at java.util.TimSort.binarySort(TimSort.java:265)
at java.util.TimSort.sort(TimSort.java:190)
at java.util.Arrays.sort(Arrays.java:727)
at TestSort.<init>(TestSort.java:48)
at TestSort.main(TestSort.java:71)

Can someone explain this to me ? 谁可以给我解释一下这个 ? Thanks. 谢谢。

This is how to solve. 这是怎么解决的。 I kept your class "structure", but I took the liberty of changing variable names to get clearer what I was doing. 我保留了您的类的“结构”,但是我自由地更改了变量名,以使我的工作更加清晰。 You asked how to order an array not changing the order of elements, but changing a index upon it. 您询问了如何对数组进行排序,而不改变元素的顺序,而是改变其上的索引。

Here's the code. 这是代码。

    public class TestSort {

       int c_maxSort = 10000;
       Integer[] unsortedAndFiltered = new Integer[c_maxSort];
       Integer[] index = new Integer[c_maxSort];

       public TestSort() {

          Integer exclude = 0;
          int newPosition;
          int lengthOfOriginalArray;


          Integer[] originalArray = { 5, 3, 0, 2, 7, 1, 5, 6, 8, 4 };
          lengthOfOriginalArray = originalArray.length;

          System.out.print("Original: ");
          for (int i = 0; i < lengthOfOriginalArray; i++)
             System.out.print(originalArray[i] + " ");
          System.out.println(" - Length: " + lengthOfOriginalArray);

          newPosition = 0;

          for (int i = 0; i < lengthOfOriginalArray; i++) {

             if (!originalArray[i].equals(exclude)) {
                index[newPosition] = newPosition;
                unsortedAndFiltered[newPosition] = originalArray[i];
                newPosition++;
             }
          }

          System.out.println("Tags: ");
          for (int i = 0; i < newPosition; i++)
             System.out.print(index[i] + " ");
          System.out.println("");
          System.out.println("Unsorted numbers: ");
          for (int l_loop = 0; l_loop < newPosition; l_loop++)
             System.out.print(unsortedAndFiltered[l_loop] + " ");
          System.out.println("");

          int deleted = lengthOfOriginalArray - newPosition;
          Arrays.sort(index, 0, newPosition, new IndirectedComparator(unsortedAndFiltered, index));

          System.out.println("Sorted Tags: ");
          for (int i = 0; i < newPosition; i++)
             System.out.print(index[i] + " ");
          System.out.println("");
          System.out.println("Sorted Numbers: ");
          for (int i = 0; i < newPosition; i++)
             System.out.print(unsortedAndFiltered[index[i]] + " ");
          System.out.println("");

       }

       public static void main(String[] args) {
          new TestSort();
       }

    }

where the comparator is coded like this: 比较器的编码如下:

public class  IndirectedComparator implements Comparator<Integer> {


   private Integer[] array;
   private Integer[] index;

   public IndirectedComparator(Integer [] array, Integer[] index){
      this.array = array;

      this.index = new Integer[index.length];
      System.arraycopy(index, 0, this.index, 0, index.length);
   }

   @Override
   public int compare(Integer i1, Integer i2) {
      return Integer.compare(array[index[i1]], array[index[i2]]);
   }

}

This is my execution: 这是我的处决:

Original: 5 3 0 2 7 1 5 6 8 4  - Length: 10
Tags: 
0 1 2 3 4 5 6 7 8 
Unsorted numbers: 
5 3 2 7 1 5 6 8 4 
Sorted Tags: 
4 2 1 8 0 5 6 3 7 
Sorted Numbers: 
1 2 3 4 5 5 6 7 8 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM