Hello,
I am trying to remove values from lists.
private List<Node> nodes; private List<Edge> edges; public List<Edge> getEdgeTo(Node x) { List<Edge> edges1 = new ArrayList<Edge>(); for (int i = 0; i < edges.size(); i++) { if (edges.get(i).getFirstNode().getId() == x.getId() || edges.get(i).getSecondNode().getId() == x.getId()) { edges1.add(edges.get(i)); } } return edges1; } private void removeEdge(Node x) { List<Edge> edges2 = getEdgeTo(x); for (int i = 0; i < edges2.size(); i++) { edges.remove(i); } } private void removeNode(Node x) { for (int i = 0; i < nodes.size(); i++) { if(nodes.get(i).getId() == x.getId()){ nodes.remove(x); } } }
But when i try to run method that uses this functions it works
incomprehensible to me.
Result of running this function looks like this:public Map<Node, Node> findNextHospital(Node start) { Node current = start; Map<Node, Node> minDistances = new HashMap<Node, Node>(); calculateShortestPathFromSource(current); Node nextHospital = getMinimumDistanceOfAll(current); System.out.println("Next hospital " +nextHospital.getId()); minDistances.put(current, nextHospital); removeNode(current); removeEdge(current); System.out.println("Before"); System.out.println("Nodes size "+ nodes.size() + "Edges size"+edges.size()); for(int i=0;i<nodes.size();i++){ System.out.println(nodes.get(i).getId()); } for(int i=0;i<edges.size();i++){ System.out.println(edges.get(i).getSecondNode().getId() + " " + edges.get(i).getFirstNode().getId()); } current = nextHospital; System.out.println("current id "+ current.getId()); calculateShortestPathFromSource(current); Node nextHospital1 = getMinimumDistanceOfAll(current); System.out.println("Next hospital "+nextHospital1.getId()); minDistances.put(current, nextHospital1); removeNode(current); removeEdge(current); for(int i=0;i<nodes.size();i++){ System.out.println(nodes.get(i).getId()); } for(int i=0;i<edges.size();i++){ System.out.println(edges.get(i).getSecondNode().getId() + " " + edges.get(i).getFirstNode().getId()); } System.out.println("After"); System.out.println(nodes.size()); System.out.println(edges.size()); return minDistances; }
Next hospital 3
Before
Nodes size 4Edges size10
1
2
3
4
3 0
2 1
4 3
0 1
0 3
3 1
1 2
1 4
3 4
4 2
current id 3
Next hospital 0
1
2
4
2 1
0 1
3 1
1 4
4 2
After
3
5
2
Numbers of nodes and edges have decreased but list still containts some edges to Node with id 0 despite of fact that all should be removed. Also node with id 0 when I check if nodes list containts it returns boolean false value. But then "Next hospital 0" has id 0 but node with this id shouldn't exist.