在实现Java字典形式时,遇见了k,v形式覆盖问题,内心很崩溃。python转到java还真的很多不习惯,最后解决方式为重新定义实例化。问题就解决了。
public HashMap<Integer, ArrayList<Integer>> getDigraphNodeMap() { HashMap<Integer, ArrayList<Integer>> nodeLink = new HashMap<>(); TreeSet<Integer> matirxLabel = new TreeSet<>(); for (int i = 0; i < graph.getVertexList().length; i++) { ArrayList<Integer> matirxValue = new ArrayList<>(); for (int j = 0; j < graph.getVertexList().length; j++) { if (graph.getAdjMat()[i][j] == 1) { matirxValue.add(j); matirxLabel.add(i); } if (matirxLabel.contains(i)) { nodeLink.put(i, matirxValue); } } } return nodeLink; }其中的下划线部分就是新定义的格式。开始的时候在for循环外进行了定义,使用清除的方式,但是内存没有清除,产生的结果是新加入的k,v会覆盖前面已定义的k,v。
分析有不对的地方,谢谢指出。谢谢!