1

I need your help, I created this two classes

public class Arch <N,T>{
    T label;
    N start;
    N end;
...constructor...

public class Graph <N,T>{
    private ArrayList<N> nodes;                                                                                    
    private HashMap<N,ArrayList<T>> arch;  //for each Node I have saved an arrayList of Archs 
                                            //connected to him                                                                        
    private int nNode;                                                                                             
    private int nArch;                                                                                             
    private boolean oriented;
...constructor..

I need a method in Graph to delete an Arch from the ArrayList that I have in the hashmap.

I tried like this in the class Graph:


public void deleteArch(N start, N end){                           
    ArrayList<Arch<N,T>> archsForNode=(ArrayList<Arch<N,T>>)arch.get(start);
    for(Arch<N,T> a : archsForNode){                                        
        if(a.start==start || a.end == end) {                                
            archsForNode.remove(a);                                         
        }                                                                   
                                                                            
    }                                                                       
} 

but when I tested it in my test main the result is that nothing changed in my ArrayList in the hashmap.

someone can help me please!

thank you all

Sof
  • 59
  • 6

2 Answers2

2

I am 90% certain that your problem's root cause is that your classes are not overriding Object#equals(Object) and Object#hashCode() methods. In order for a remove() function to work, it needs to compare the object being passed to the function against members of the collection you are iterating over so that the object that's equal to the argument is removed.

I suggest you research overriding these methods for proper implementation. But, in summary, your classes must implement these methods and in the case of complex classes (classes containing other object references) each one of these classes must override these methods. The reason why classes like String work in these cases is because the String class overrides these methods. For your custom classes, you must do the same. Think about this logically. How could a collection magically remove an object if there is no way for the code to decide which objects are equal to what you are looking for?

hfontanez
  • 5,774
  • 2
  • 25
  • 37
2

I suspect that deleteArch throws a ConcurrentModificationException because you are modifying archsForNode while iterating over it:

   for(Arch<N,T> a : archsForNode){                                        
        if(a.start==start || a.end == end) {                                
            archsForNode.remove(a);                                         
        }                                                                                                                                             
    } 

This is not allowed.
For more information see this Q&A.

c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1
    This is also important. Beware of concurrent modification exceptions. The best thing to do in these cases is use the `Iterator` of the collection and let it modify (remove in your case) the member of the collection that needs to be removed. – hfontanez Nov 30 '21 at 19:31