Java Array通过比较器排序

时间:2013-11-17 22:42:32

标签: java arrays sorting nullpointerexception comparator

完整的代码可在以下网址找到:https://skydrive.live.com/redir?resid=7B7D2F11B13EF9C9!54468&authkey=!AD4fD8sGgc7oJIE

这是代码的一部分:

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是'Integer'。 g_exclude用于排除不得作为排序一部分的项目。

如果没有排除任何项目(没有项目的值等于g_exclude或if-statement被注释),一切正常。

当排除一个或多个项目时,我得到一个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)

有人可以向我解释一下吗?感谢。

1 个答案:

答案 0 :(得分:0)

这是如何解决的。我保持你的班级“结构”,但我冒昧地改变变量名称,以更清楚我正在做什么。您询问如何订购数组而不是更改元素的顺序,而是更改元素的索引。

这是代码。

    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();
       }

    }

比较器编码如下:

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]]);
   }

}

这是我的执行:

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