Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:ArrayList的高级比较_Java_Android_Arraylist_Comparator - Fatal编程技术网

Java Android:ArrayList的高级比较

Java Android:ArrayList的高级比较,java,android,arraylist,comparator,Java,Android,Arraylist,Comparator,我有arraylista,它包含100个随机项[0:100]项,可以重复 另外,我还有int b=50。我想按照这个表达式的结果的升序对数组列表中的项目进行排序: Math.abs (ArrayList.get(index) - b); 例如: 6584331877…-ArrayList之前 15 34 17 32 27-Math.abs(ArrayList.get(index)-b)之前的 65 33 77 18 84…-ArrayList之后 15172734-Math.abs(Array

我有
arraylista
,它包含100个随机项[0:100]项,可以重复

另外,我还有
int b=50
。我想按照这个表达式的结果的升序对数组列表中的项目进行排序:

Math.abs (ArrayList.get(index) - b);
例如:

6584331877…
-ArrayList之前

15 34 17 32 27
-Math.abs(ArrayList.get(index)-b)之前的

65 33 77 18 84…
-ArrayList之后

15172734
-Math.abs(ArrayList.get(index)-b)之后(按升序排列)


我想,我可以使用java.util.Comparator来实现这一点,但我仍然不明白它是如何工作的。有人能解释一下吗?或者,也许还有其他方法?谢谢!

为以下内容提供一个自定义
比较器

final int b=。。。;
Collections.sort(myList,newcomparator(){
@凌驾
public void compareTo(整数i1、整数i2){
整数realI1=(int)Math.abs(i1-b);
整数realI2=(int)Math.abs(i2-b);
返回realI1.compareTo(realI2);
}
});

为以下各项提供自定义的
比较器

final int b=。。。;
Collections.sort(myList,newcomparator(){
@凌驾
public void compareTo(整数i1、整数i2){
整数realI1=(int)Math.abs(i1-b);
整数realI2=(int)Math.abs(i2-b);
返回realI1.compareTo(realI2);
}
});

是的,您可以使用比较器

class MyComparator implements Comparator<Integer> {

    private final int b;

    public MyComparator(int b) { this.b = b; }

    public int compare(Integer o1, Integer o2) {
        Integer i1 = Math.abs(o1 - b);
        Integer i2 = Math.abs(o2 - b);
        return i1.compareTo(i2);
    }
}

这将根据比较器中的标准对
整数
列表进行排序。

是的,您可以使用比较器

class MyComparator implements Comparator<Integer> {

    private final int b;

    public MyComparator(int b) { this.b = b; }

    public int compare(Integer o1, Integer o2) {
        Integer i1 = Math.abs(o1 - b);
        Integer i2 = Math.abs(o2 - b);
        return i1.compareTo(i2);
    }
}

这将根据比较器中的标准对
整数
列表进行排序。

将b设为最终值,以避免不必要的副作用,这正是我想让您推断的。现在从
b
中删除
静态
修饰符,否则它将无法编译。是的,它应该是。您将我与该注释混淆了。将b设为最终值为了避免不必要的副作用,这正是我想让你们推断的。现在从
b
中删除
static
修饰符,否则它将无法编译。是的,它应该是。你们把我和那个评论搞混了。
class MyComparator implements Comparator<Integer> {

    private final int b;

    public MyComparator(int b) { this.b = b; }

    public int compare(Integer o1, Integer o2) {
        Integer i1 = Math.abs(o1 - b);
        Integer i2 = Math.abs(o2 - b);
        return i1.compareTo(i2);
    }
}