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) :如果当前的
    模板列表长度与当前的最大大小相同,则只需添加另一个
    endKey
    (这也将处理长度为2的任何“第一”路径)
  • (!!):这可能包含可能导致找不到长度至少为2的路径的错误

你调试过你的代码吗?如果最长路径不超过2个段,你的代码将返回空列表,因为你在开始时有“max=2”。很难判断所有看起来过于复杂的嵌套会出现什么问题。为什么你要迭代两次?除非我遗漏了什么,否则我会说一次迭代应该是en即使是未排序的列表。如果遇到新的最大值,则清除结果列表并将当前元素添加到列表中,否则,如果当前元素的距离等于当前最大值,则添加当前元素。
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);
}