Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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 我的ArrayList中的唯一值_Android_Arraylist_Unique - Fatal编程技术网

Android 我的ArrayList中的唯一值

Android 我的ArrayList中的唯一值,android,arraylist,unique,Android,Arraylist,Unique,我有下节课: private class LocoList{ String Id; String Name; String Latitude; String Longitude; LocoList(String LocationId, String LocationName, String LocationLatitude, String LocationLongitude){ this.Id = LocationI

我有下节课:

private class LocoList{
    String Id;
    String Name;
    String Latitude;
    String Longitude;

    LocoList(String LocationId, String LocationName,  String 
        LocationLatitude, String LocationLongitude){

        this.Id = LocationId;
        this.Name = LocationName;
        this.Latitude = LocationLatitude;
        this.Longitude = LocationLongitude;
        }

    @Override public boolean equals(Object obj) {
        if (obj == this) { return true; }
        if (obj == null || obj.getClass() != this.getClass()) { return false; }
        NearLocoList guest = (NearLocoList) obj;
        return Id == guest.Id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(Id);
    }

}
要与下一个阵列一起使用:

private ArrayList<LocoList> myLocations = new ArrayList<>();
private ArrayList myLocations=new ArrayList();
要填充我的arrayList,我使用下一种方法:

private void getLocoDatabase(LatLng PoI) {

    double mLatitude, mLongitude;
    mLatitude = PoI.latitude;
    mLongitude = PoI.longitude;

    ArrayList<ArrayList<Object>> dataLocation =
            mDBhelperMA.getLocations(mLatitude, mLongitude);

   for (int i = 0; i < dataLocation.size(); i++) {
        ArrayList<Object> row = dataLocation.get(i);

  if (!myLocations.contains(new LocoList(row.get(0).toString(), row.get(1).toString(), row.get(2).toString(),row.get(3).toString) {
        myLocations.add(new LocoList(row.get(0).toString(), row.get(1).toString(), row.get(2).toString(),row.get(3).toString);
        }
    }
}
private void getLocoDatabase(LatLng PoI){
双倍耐力,长度;
M纬度=点纬度;
m长=点经度;
ArrayList数据位置=
获得位置(高度、长度);
对于(int i=0;i
我的问题是,对于不同的LatLng,需要多次调用getLocaodatabase,并希望避免重复的值


我必须做什么才能使myLocations ArrayList上的值唯一?要避免相同位置出现两次或两次以上。

在LocolList中重写
equals
方法,并决定两个LocolList对象被视为相等的条件

然后,在将对象添加到arraylist时,执行
.contains
检查并仅在值不存在时添加

--编辑--

如何使用equals

@Override 
public boolean equals(Object obj) {        
    if (obj != null) { 
        LocoList compareObj = (LocoList) obj;
        if (compareObj.id == id)
            return true;
    }
    return false;
}
如何使用contains

LocoList myObj = new LocoList(row.get(0).toString(), row.get(1).toString(),
                      row.get(2).toString(), row.get(3).toString;
if (!myLocations.contains(myObj) {
    myLocations.add(myObj);
}

可以使用“设置”来选择列表

Set<String> set = new LinkedHashSet<>(list);
Set Set=newlinkedhashset(列表);

Set Set=newlinkedhashset;
set.addAll(列表);
list.clear();
list.addAll(集合)

你必须做两件事:

  • 默认值等于()/hashCode()实现
  • 例如,从使用List/ArrayList更改为Set/HashSet

您必须了解不同的集合类具有不同的属性。当“唯一性”这是您最关心的问题,然后使用集合,而不是列表。

可以使用一个
集合
已经测试过,但仍然得到了两个值。我需要为列表中的LocalList获取uniques Id。不要使用==,使用
equals
。所以将
Id==guest.Id
更改为
Id.equals(guest.Id)
(如果
Id
可能为空,则使用空检查)。这是周末。对不起!我感谢您的快速回复:-)我已经测试过并得到了重复项。可能是因为我使用了类而不是字符串。我测试并更新了问题代码。仍然得到了双值。你告诉他使用==来测试等式。他应该改为使用
equals
 Set<String> set = new LinkedHashSet<>;
set .addAll(list);
list.clear();
list.addAll(set )