Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8589143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:58:40+00:00 2026-06-11T22:58:40+00:00

In the past, I’ve been able to implement a generic ArrayList by casting Object[]

  • 0

In the past, I’ve been able to implement a generic ArrayList by casting Object[] to E[]:

public class ArrayList<E>
{
    private E[] elements;

    @SuppressWarnings("unchecked")
    public ArrayList()
    {
        elements = (E[])new Object[16];
    }
 }

and I could use the generic array normally, like so:

public void add(E element)
{
    ...
    elements[size++] = element;
}

I thought I could implement a trie similarly:

public class Trie<V>
{
    private static class Node<V>
    {
        public V value;
        @SuppressWarnings("unchecked")
        public Node<V>[] next = (Node<V>[])new Object[26];
    }

    private Node<V> root = new Node<V>();
}

However, the line public Node<V>[] next = (Node<V>[])new Object[26]; in Trie.Node causes a ClassCastException:

public class Main
{
    public static void main(String[] args)
    {
        ArrayList<Integer> a = new ArrayList<Integer>();
        Trie<Integer> t = new Trie<Integer>();
    }
}

java Main
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LTrie$Node;
    at Trie$Node.<init>(Trie.java:6)
    at Trie$Node.<init>(Trie.java:3)
    at Trie.<init>(Trie.java:10)
    at Main.main(Main.java:6)

So it’s saying I can’t cast Object[] to Trie.Node[].

What’s the difference between casting from Object[] to E[] and casting from Object[] to Trie.Node<V>[]? Why is the former permitted and not the latter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:58:41+00:00Added an answer on June 11, 2026 at 10:58 pm

    The difference is that generics are implemented via erasure, and giving no bounds will erase that generic type into Object. E[] will become Object[], while Trie.Node<V>[] will become Trie.Node<Object>[]. Hence, (Node<V>[])new Object[26]; is a invalid cast as you’re trying to cast Object[] to Node<Object>[].

    You can find more information about how erasure works at http://docs.oracle.com/javase/tutorial/java/generics/erasure.html.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the past I've been able to modify the CSS on a page via
In the past I'd read tons of code with methods like: public Object doSomething()
for past two hours i've been trying to pass responseText to the object i'm
For the past day I have been trying to figure out how to change
For the past 2 days I've been struggling trying to successfully authenticate users into
These past few days I've been working toward converting my PHP code base from
The past week or two I've been studying Maven, and I like it, but
For past 7 months I have been designing web applications using open source technologies
This past semester I took intro to object oriented programming in java and next
For the past few days I've been writing classes that at first I thought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.