2012-05-16 21 views
0

我有一个数组,而不是一个arrayList,我想对它排序。 这只能告诉我,集合是不适用的参数(pixelPlaceSort.Pixel [],...等等。排序数组而不是ArrayList

Collections.sort(pixels, new PixelComparator()); 

我可以用一个列表来解决,但对于学习的目的我不知道希望这样。

那么如何才能这项工作? c是一个int。

class PixelComparator implements Comparator<Pixel> { 

    PixelComparator() { 
    } 

    public int compare(Pixel p1, Pixel p2) { 
    if(p1.c < p2.c) { 
     return -1; 
    } else if(p1.c > p2.c) { 
     return 1; 
    } 
    else { 
     return 0; 
    } 
    } 


} 
+2

下次试试Google! “java排序数组”可能已经工作 –

+0

吨重复:http://stackoverflow.com/questions/1694751/java-array-sort-descending,http://stackoverflow.com/questions/215271/sort-arrays-of -primitive-type-in​​-des-order-order等等 – kostja

+0

我真的做了搜索!主要发现了Integer将用于解决问题或转换为列表并转换回来的东西。但也许我应该搜索一些更长的时间,谢谢那个 – clankill3r

回答

1

对于您有一个名为阵列类。

使用Arrays.sort(像素) 或者Arrays.sort(像素,新PixelComparator())

检查javadoc为java.util.Arrays中,并选择排序的适当形式()方法。

6

可以使用Arrays.sort(Object[] array)Arrays.sort(Object[] array,Comparator c)

+0

谢谢,伟大的作品 – clankill3r

0

你有2个选项来排序。

1保持你的阵列:

您使用sort method of the "Arrays" Class

Arrays.sort(pixels,new PixelComparator()); 

2-使用,而不是一个列表

您首次在数组转换成列表,然后对其进行分类:

ArrayList<Pixel> pixellist = new ArrayList<Pixel>(Arrays.asList(pixels)); 
Collections.sort(pixellist, new PixelComparator());