I am writing a method that creates an ArrayList S_Comp that contains all elements which are included in the array allNodes, but which are not included in the ArrayList solution (whose elements are all included in allNodes). When I print solution and allNodes, I can easily see that S_Comp should be containing 7 elements. However, all elements from allNodes are added to S_Comp, also the elements which are included in nodesS. What could be the problem here?
Below you can find my code for the method. Before calling this method, S_Comp has only been initialized as new ArrayList<MyNodesData>, so its size is 0.
public void generateSComp(DataFile testDataFile, ArrayList<Route> solution, ArrayList<MyNodesData> S_Comp)
{
System.out.println("solution: " + solution.toString());
ArrayList<MyNodesData> nodesS = getNodesS(solution);
System.out.println("NodesS: " + nodesS.toString());
System.out.println("Size nodesS: " + nodesS.size());
MyNodesData[] allNodes = testDataFile.getNodes();
System.out.println("allNodes: " + Arrays.toString(allNodes));
for(MyNodesData node : allNodes)
{
if(!nodesS.contains(node))
{
System.out.println(node.toShortString() + " is not in nodesS");
S_Comp.add(node);
}
}
containsis using reference equality. If the objects are distinct instances with the same fields thencontainswill return false.