4

I have this bit of code and it keeps saying that it cannot create a generic array, but, I don't have a generic in my Node class, just an Object field. The Node class is an inner class. Why is it doing this?

public class TernarySearchTrie<E> implements TrieInterface<E> {

    private Node[] root = new Node[256];
    private int size = 0;

    private class Node {
        char c;
        Node left, mid, right;
        Object value;
    }
}
2
  • Where is Node defined? Commented May 23, 2016 at 0:43
  • private TernarySearchTrie.Node[] root = new TernarySearchTrie.Node[256]; works Commented May 23, 2016 at 0:46

2 Answers 2

4

Add the static modifier to Node class:

private static class Node {
    char c;
    Node left, mid, right;
    Object value;
}

Without static, it depends of the TernarySearchTrie class, that have generics.

Sign up to request clarification or add additional context in comments.

Comments

2

What you do in the problematic new Node[256] is actually TernarySeachTrie<E>.Node[256]. One solution is to use raw type:

Node[] root = TernarySearchTrie.Node[256];

Of course the compiler gives you a warning for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.