1700444870
1700444871
return true;
1700444872
1700444873
}
1700444874
1700444875
if(obj.getClass()!=getClass()){
1700444876
1700444877
return false;
1700444878
1700444879
}
1700444880
1700444881
City city=(City)obj;
1700444882
1700444883
//根据code判断是否相等
1700444884
1700444885
return new EqualsBuilder()
1700444886
1700444887
.append(code, city.code)
1700444888
1700444889
.isEquals();
1700444890
1700444891
}
1700444892
1700444893
}
1700444894
1700444895
与上一个建议类似,把多个城市对象放在一个List中,然后使用不同的方法查找同一个城市,看看返回值有什么异常。代码如下:
1700444896
1700444897
public static void main(String[]args){
1700444898
1700444899
List<City>cities=new ArrayList<City>();
1700444900
1700444901
cities.add(new City(“021”,“上海”));
1700444902
1700444903
cities.add(new City(“021”,“沪”));
1700444904
1700444905
//排序
1700444906
1700444907
Collections.sort(cities);
1700444908
1700444909
//查找对象
1700444910
1700444911
City city=new City(“021”,“沪”);
1700444912
1700444913
//indexOf方法取得索引值
1700444914
1700444915
int index1=cities.indexOf(city);
1700444916
1700444917
//binarySearch查找到索引值
1700444918
1700444919
int index2=Collections.binarySearch(cities, city);
[
上一页 ]
[ :1.70044487e+09 ]
[
下一页 ]