在實現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。
分析有不對的地方,謝謝指出。謝謝!