Java 返回空数组 static ArrayList furthestAirport(出发机场内部){ int max=2,currentSize=0; ArrayList结果=新建ArrayList(); ArrayList tempList=新的ArrayList(); for(int-endKey:stationkey){ 如果(离开机场!=结束键){ HashSet tempHash=g.shortestpath(离开机场,结束键); 如果(!tempHash.isEmpty()){ for(ArrayList临时值:tempHash){ currentSize=温度大小(); 如果(max
,我将对我的评论进行一点扩展,并添加一些代码以使其更清晰:Java 返回空数组 static ArrayList furthestAirport(出发机场内部){ int max=2,currentSize=0; ArrayList结果=新建ArrayList(); ArrayList tempList=新的ArrayList(); for(int-endKey:stationkey){ 如果(离开机场!=结束键){ HashSet tempHash=g.shortestpath(离开机场,结束键); 如果(!tempHash.isEmpty()){ for(ArrayList临时值:tempHash){ currentSize=温度大小(); 如果(max,java,graph,Java,Graph,,我将对我的评论进行一点扩展,并添加一些代码以使其更清晰: static ArrayList<Integer> furthestAirport(int departingAirport) { int max = 2, currentSize = 0; ArrayList<Integer> result = new ArrayList<Integer>(); ArrayList<Integer> tem
static ArrayList<Integer> furthestAirport(int departingAirport) {
int max = 2, currentSize = 0;
ArrayList<Integer> result = new ArrayList<Integer>();
ArrayList<Integer> tempList = new ArrayList<Integer>();
for (int endKey : stationKeys) {
if (departingAirport != endKey) {
HashSet<ArrayList<Integer>> tempHash = g.shortestPaths(departingAirport, endKey);
if (!tempHash.isEmpty()) {
for (ArrayList temp : tempHash) {
currentSize = temp.size();
if (max <= currentSize) {
max = currentSize;
if (!tempList.contains(endKey))
tempList.add(endKey);
}
}
}
}
}
for (int endKey : tempList) {
if (departingAirport != endKey) {
HashSet<ArrayList<Integer>> tempHash = g.shortestPaths(departingAirport, endKey);
if (!tempHash.isEmpty()) {
for (ArrayList temp : tempHash) {
currentSize = temp.size();
if (max == currentSize) {
if (!result.contains(endKey))
result.add(endKey);
}
}
}
}
}
return result;
}
furthestAirport静态列表(出发机场内部){//(1)
int max=2;
Set result=new LinkedHashSet();/(2)
for(int-endKey:stationkey){
如果(离开机场!=endKey){
//(!!)
设置tempHash=g.shortestpath(出发机场,结束键);//(1)
如果(!tempHash.isEmpty()){//会发生这种情况吗?
for(列表模板列表:tempHash){
if(tempList.size()>max){//(3)
result.clear();
结果.添加(endKey);
}else if(templast.size()==max){//(4)
结果.添加(endKey);
}
}
}
}
}
返回新的ArrayList(结果);
}
注:
- (1) :为变量使用接口,以便能够更改所使用的实现
- (2) :使用LinkedHashSet允许保留插入顺序,并允许集合处理重复项(不会添加)
- (3) :如果当前的
大于当前的最大大小,请删除到目前为止可能收集到的任何内容,因为它只能更短。然后添加当前的模板列表
,因为它表示“到目前为止”最远的机场结束键
- (4) :如果当前的
模板列表长度与当前的最大大小相同,则只需添加另一个
(这也将处理长度为2的任何“第一”路径)endKey
- (!!):这可能包含可能导致找不到长度至少为2的路径的错误
static List<Integer> furthestAirport(int departingAirport) { //(1)
int max = 2;
Set<Integer> result = new LinkedHashSet<>(); //(2)
for (int endKey : stationKeys) {
if (departingAirport != endKey) {
//(!!)
Set<List<Integer>> tempHash = g.shortestPaths(departingAirport, endKey); //(1)
if (!tempHash.isEmpty()) { // should that ever happen?
for (List<Integer> tempList : tempHash) {
if( tempList.size() > max ) { //(3)
result.clear();
result.add(endKey);
} else if( tempList.size() == max ) { //(4)
result.add(endKey);
}
}
}
}
}
return new ArrayList<>(result);
}