I am new to Java and this is probably really easy, but I am stuck with it. I have to create class Node which represents all nodes in a graph. I will use it to implement basic algorithms like dfs. I need to use only one field which is ArrayList of all neighbours of a node. Each created node gives a reference to its neighbours. This is my class so far:
public class Node{
protected ArrayList<Node> neighbours = new ArrayList<>();
public Node(ArrayList<Node> neighbours){
for(int i=0;i<neighbours.size();i++){
this.neighbours.add(neighbours.get(i));
}
}
public void setNeighbours(ArrayList<Node> neighbours){
this.neighbours=null;
}
public ArrayList<Node> getNeighbours(){
return this.neighbours;
}
}
In this case I have Node A = new Node([B,C]), for example, which is OK. Sometimes I want to use A not as ArrayList [B,C], but simply as Node A and it should still point to [B,C]. Is this achievable? How could I do it? Probably my class needs one more constructor for this? Any help really appreciated, thanks.