Home > database >  Java keeps trying to convert my object to int
Java keeps trying to convert my object to int

Time:05-11

I'm writing up a breadth-first-search algorithm, however when i go to construct my graph i get the error breadth.java:68: error: incompatible types: Node cannot be converted to int: v8.children.add(v8, v7);

This happens when i try to add Node objects to another Node's ArrayList for it's child nodes.

I feel the solution may be one of those blindingly obvious things that just require a second pair of eyes to locate the error, hence me posting it here.

Here is my full code:

import java.util.*;
import java.io.*;


public class breadth{

    static class Node {

        int val;
        List<Node> children = new ArrayList<>();

        public Node(int value){
            val = value;
        }

    }

    public boolean bfs(Node start, int end){
        HashSet<Node> visited = new HashSet<>();
        Queue<Node> frontier = new LinkedList<>();
        frontier.add(start);


        while (!frontier.isEmpty()){
            Node current = frontier.remove();
            if(current.val == end){
                System.out.println("Path to goal located");
                return true;
            }

            for (int i = 0; i < current.children.size(); i  ){
                Node nextNode = current.children.get(i);
                if(!visited.contains(nextNode)){
                    frontier.add(current.children.get(i));
                }

            }

            visited.add(current);
        }

        return false;
    }


    public static void main(String[] args){

        // Create the Node objects

        Node v1 = new Node(1);
        Node v2 = new Node(2);
        Node v3 = new Node(3);
        Node v4 = new Node(4);
        Node v5 = new Node(5);
        Node v6 = new Node(6);
        Node v7 = new Node(7);
        Node v8 = new Node(8);

        // Link the nodes by setting their children. This is based off the vacuum problem

        v1.children.add(v1, v2, v3);
        v2.children.add(v2, v1, v6);
        v3.children.add(v3, v4);
        v4.children.add(v4, v3, v8);
        v5.children.add(v5, v6, v7);
        v6.children.add(v6, v5);
        v7.children.add(v7, v8);
        v8.children.add(v8, v7);

    }
}

The thing is, im under the impression that I didn't once specify a type as an int, other than the value variable. Everything is of type Node, what's the problem here?

I have also tried declaring the node objects differently, to no avail.

Node<Integer> v1 = new Node(1); Node<Integer> v1 = new Node<>(1);

I can only assume that it's where im passing an integer to the arguments, however this is just for the constructor to change the val.

CodePudding user response:

I'm not sure, because I would need to see the stacktrace, but it appears to me, that it happens at those add(v1, v2, v3) calls.

The thing is, the add-method of ArrayList does not support such an adding where you just list all the items to add (I might be a bit out of date with the newer Java-versions though).

So what you rather need is to split those up like follows:

v1.children.add(v1);

v1.children.add(v2);

v1.children.add(v3);

or easier: v1.children.add(List.of(v1, v2, v3)); (props to Ghostcat)

This would also fit the problem, because if you give two arguments to add, the first would be the index of where to insert it. So here it would really try to perfrom a cast to int.

If this does not help you, please add a full stack trace of what is going wrong.

Edit: I overlooked, that you even mentioned, that it happens during the adding-process, so I'm actually pretty sure, that that is the solution.

CodePudding user response:

Christian is right, when I try to compile it, the first error I get is:

➜  stackoverflow javac breadth.java
breadth.java:61: error: no suitable method found for add(Node,Node,Node)
        v1.children.add(v1, v2, v3);

One additional thing you might like to look at, to add multiple items to the ArrayList at once, is Arrays.asList():

        v1.children.addAll(Arrays.asList(v1, v2, v3));

CodePudding user response:

If you want to use a graph, then you should use a graph, not an ArrayList.

You use Generic ArrayList Class's .add(int index, T element) method. This method adds the element to the given index of the arraylist but what you want is add edges between to vertices.

For this aim, you may create a Graph class, use map, hashmap etc. but not ArrayList. Actually, the whole structure is designed the wrong way in your code, so I couldn't edit and give you a solution in the code. Some of the other answers are to simulate Graph with ArrayList and works but it is not the proper way(Also it may cause more confusion for beginners). You should create a new design that you only use a proper data structure. Here is a good point to start.

  •  Tags:  
  • java
  • Related