Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.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
Android 获取对象的最大值_Android_Arraylist - Fatal编程技术网

Android 获取对象的最大值

Android 获取对象的最大值,android,arraylist,Android,Arraylist,我有个小问题。我从多个信标获取数据,并将其放入arrayList。现在我想把最近的一个还给你 ArrayList<Beacon> arrayList = new ArrayList<Beacon>(); String beaconName; public void addToBeaconInfo(Beacon beacon) { boolean beaconAlreadyInList = false; //arrayList.add(beacon);

我有个小问题。我从多个信标获取数据,并将其放入arrayList。现在我想把最近的一个还给你

ArrayList<Beacon> arrayList = new ArrayList<Beacon>();
String beaconName;

public void addToBeaconInfo(Beacon beacon) {

    boolean beaconAlreadyInList = false;
    //arrayList.add(beacon);
    beaconName = "" + beacon.getId2() + "" + beacon.getId3();
    //Log.i("BeaconInfo","addBeacon_beaconName: "+beaconName + " distance" + beacon.getDistance());

    int i;
    for(i = 0; i < arrayList.size(); i++){
        Beacon tempBeacon = arrayList.get(i);

        if(tempBeacon.getId2().equals(beacon.getId2())){
            beaconAlreadyInList = true;
            arrayList.remove(beacon);

            Log.i("BeaconInfo", "addBeacon_tempBeacon: " + beacon.getId3() + " distance: " + beacon.getDistance());
            break;
        }
    }
    if(beaconAlreadyInList == false){
        arrayList.add(beacon);
        Log.i("arrayList","" + beacon.getDistance() + " " + beacon.getBluetoothName());
        double maxDistance = beacon.getDistance();
        //double a = Collections.max(maxDistance);
        //Log.i("maxDistance","" + Math.max());
    }
}
ArrayList ArrayList=new ArrayList();
字符串beaconName;
公共void addToBeaconInfo(信标){
布尔beaconAlreadyInList=false;
//arrayList.add(信标);
beaconName=“”+beacon.getId2()+“”+beacon.getId3();
//Log.i(“BeaconInfo”、“addBeacon\u beaconName:+beaconName+“distance”+beacon.getDistance());
int i;
对于(i=0;i
信标最近=空;
双倍距离=双倍最大值;
用于(信标:arrayList){
double maxDistance=beacon.getDistance();
if(最大距离<距离){
距离=最大距离;
最近=信标;
}
}

尝试使用
https://developer.android.com/reference/android/support/v7/util/SortedList.html
感谢您的快速请求,但这只适用于两个信标。在我的例子中,我有一个可变数量的信标,我想按他的距离排序。你在问题中写道,你需要最近的信标。如果需要排序,只需使用其中任何一个即可,可以同时使用两个;)但不止两个信号灯,可能是我的错。我现在用不同的ArrayList和集合制作了它。但真的很感谢你的帮助;)
Beacon nearest = null;
double distance = Double.MAX_VALUE;
for (Beacon beacon: arrayList){
  double maxDistance = beacon.getDistance();
  if (maxDistance < distance) {
    distance = maxDistance;
    nearest = beacon;
  }
}