如何打印出HashSet类型的索引>?
以下是代码:
HashSet<ArrayList<Integer>> possibleRoutes;
我试过
int indexOfMinDistance = routesDistances.indexOf(Collections.min(routesDistances));
System.out.println(possibleRoutes.indexOf(indexOfMinDistance));
该程序建议将possibleRoutes转换为List,因为它们的类型不同,会产生更多错误。那么如何打印索引,例如。 ArrayList<Integer>, of a HashSet<ArrayList<Integer>>?
答案 0 :(得分:0)
如果您正在研究如何迭代哈希集,这可能会对您有所帮助..
HashSet<ArrayList<Integer>> possibleRoutes = new HashSet<ArrayList<Integer>>();
Iterator<ArrayList<Integer>> possList =possibleRoutes.iterator();
/* here u can't take out by index, hasset
makes no guarantees as to the iteration order of the set*/
ArrayList<Integer>main = possList.next(); //but can loop through it by next which gives ArrayList<integer>
for(Integer m : main)
{ System.out.println(m); }