Question

I'm not sure if what I want to do is feasible, but I'll explain the reasoning up front and then what I'm trying to do.

I'm using a framework for evolutionary computation that is essentially generating a tree structure from the constructor I'll be describing, where each node can have a variable amount of child nodes.

I'm trying to code up a generic node constructor that accepts a variable amount of parameters, which serve as the child objects to that node (so a node can have from 1 to children based on this ellipsis).

If I were to do this by hand for 2 children, I'd have a function like the following, where super is calling the parent class which creates the overall structure for the current node:

public Wrapper(final SageProgram sp, final Node child1, final Node child2) {
        super(child1, child2);
}

However, as I mentioned, I'd like to extend this, so I'm attempting the following:

public NWrapper(final ArrayList<Node> children) {
        super(children.toArray(new Node[children.size()]));
}

The problem is that I'm getting a java.lang.ArrayStoreException, as the variables in the constructor turn out to be array types (Node[]) instead of the singular type (Node).

Is there any way around this?

Was it helpful?

Solution

Perhaps instead of:

    public NWrapper(final ArrayList<Node> children) 

You can break the objects into Node[] objects and Node objects. Then process them inside your wrapper. The shell would then be something like the following:

    public NWrapper(final ArrayList<Node[]> children_lists, final ArrayList<Node> children)

You will still have to handle your call to your super class as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top