Iva
Iva

Reputation: 367

Java: Object that gives reference to ArrayList<Objects> from the same class

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.

Upvotes: 0

Views: 556

Answers (6)

bitli
bitli

Reputation: 573

If you change the constructor parameter to Array like this:

public Node(Node... nodes){ }

you can pass something like this:

Node A = new Node(b,c);

inside the constructor you should write like this: Arrays.asList(nodes) and it returns List

Upvotes: 0

santiago92
santiago92

Reputation: 297

Not sure answer your question but the first step is to define the graph through the edges, like a linked list, indicating the source node and the destination node. Then to perform the algorithm dfs you got that mark the path neighbor, not again pass by him, so that each node should have an attribute name and a state.

public class DephFirstSearch {
            Vertex vertice[];
            ArrayList list=new ArrayList();
            public DephFirstSearch(int  tam,ArrayList list)
            {
                    vertice = new Vertex [tam];
                    vertice[0]= new Vertex("V0", false);
                    vertice[1]= new Vertex("V1", false);
                    vertice[2]= new Vertex("V2", false);
                    vertice[3]= new Vertex("V3", false);
                    vertice[4]= new Vertex("V4", false);
                    vertice[5]= new Vertex("V5", false);
                    vertice[6]= new Vertex("V6", false);

                    this.list=list;                 

            }
            public void dfs(String origen) {

                    dfsVisit(vertice[0], 0);
            }
            public void dfsVisit(Vertex nodo, int i) {
                    nodo.setState(true);
                    System.out.println("nodo: " + nodo.getNode());

                    for (int j = 0; j < vertice.length; j++) {
                            if (isEdge(i, j) && vertice[j].isState() != true) {
                                    dfsVisit(vertice[j], j);
                            }
                    }

            }

            public boolean isEdge(int i, int j) {
                    Vertex actual = vertice[i];
                    Vertex next = vertice[j];
                    Vector lac;

                    for (int k = 0; k < list.size(); k++) {
                            lac = (Vector) list.get(k);
                            if (lac.getOrigin().equals(actual.getNode())
                                            && lac.getDestination().equals(next.getNode())) {
                                    return true;

                            }

                    }

                    return false;

                }

    public class Vertex 
  {
            String node;
            boolean state;

            public Vertex(String node, boolean state) {
                    super();
                    this.node = node;
                    this.state = state;`enter code here`
            }
}

Upvotes: 0

Durandal
Durandal

Reputation: 5663

Yes you can create a default constructor.

public Node() {}

That way you can just create an empty node

Node A = new Node();

And still use the class as you described

Node A = new Node(listOfNeighbors);

Also for your set method for your newly created empty nodes, I'd recommend using something like Bohemian posted, ie copying the list contents passed in after clearing your member lists contents: https://stackoverflow.com/a/20847517/772385

Upvotes: 1

a1j9o94
a1j9o94

Reputation: 23

I'm not sure this is completely what you're asking, but what you probably need is some kind of data field in each node. That way you can identify the Node based on it's data as opposed just a list of the nodes it connects to. As it stands now, you would only be able to search through then based on their connections because they don't have any identifying properties.

Upvotes: 0

piticent123
piticent123

Reputation: 303

If I'm interpreting your question correctly, you've already done it. If you want to refer to Node A as Node A, simply put A (the name of the variable) in your code. When you want its neighbours, simply put A.getNeighbours() in.

Node A is a Node. The neighbours are a mere property (field) of that node. This means that referring to the node by its name means it is being used as a node. Using it in tandem with getNeighbours() means it is being used as an array (in your example, [B, C])

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

Prefer not using the list passed in, but rather copy its contents:

public void setNeighbours(ArrayList<Node> neighbours){
    this.neighbours.clear();
    this.neighbours.addAll(neighbours);
}

because if you use it, another process could change it without your class knowing about the change.

Upvotes: 1

Related Questions