possibleRoutes
类型为HashSet<ArrayList<Integer>>
。 possibleRoutes
中的每个数组均列出包含按行驶顺序相互连接的地铁站或火车站的 ID。
for( ArrayList<Integer> route : possibleRoutes) {
ArrayList<Double> routesDistances = new ArrayList<Double>(); // list of the total distances of the routes
double distance = 0;
for (int i=1; i < route.size()-1; i++){
double x = Math.abs(stationLocations.get(route.get(i)).getX() - stationLocations.get(route.get(i-1)).getX());
double y = Math.abs(stationLocations.get(route.get(i)).getY() - stationLocations.get(route.get(i-1)).getY());
double d = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
distance += d;;
}
routesDistances.add(distance);
System.out.print(routesDistances);
}
这是到目前为止的输出:
[2163.9082950470897][3494.746239392733][2099.5269818921306][2075.3141294013]
我想将列表打印为数组列表,我可以从列表中选择一个索引,例如 routesDistances.get(0)
至于第一个索引。如何使列表成为 ArrayList<Double>
类型并返回为:
[2163.9082950470897, 3494.746239392733, 2099.5269818921306, 2075.3141294013]
最佳答案
直接放
ArrayList<Double> routesDistances = new ArrayList<Double>();
before
for( ArrayList<Integer> route : possibleRoutes)
循环后,您的routesDistances将如下所示:[2163.9082950470897, 3494.746239392733, 2099.5269818921306, 2075.3141294013]
然后一个循环中的输出就这样写:
for(Double d:routesDistances){
System.out.println(d);
}
关于java - 打印出一个数组列表,并选择 1 的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20533230/