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 6784643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:01:14+00:00 2026-05-26T17:01:14+00:00

Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have

  • 0

Possible Duplicate:
Java how to: Generic Array creation
Error generic array creation

I have been tasked with writing a Hash Table in Java, which must work with any data type. The rules on the code I am writing are as follows:
– The hash table must have an array as the underlying data structures, of a size determined at the time the object is constructed
– When a collision occurs, the element that collides should be placed into a linked list, which holds all of the elements at that index (key) in the hash table

Thus, for the underlying data type, I have made an array of type LinkedList (custom, not the Java API LinkedList).

private LinkedList<T>[] table;

The issue is, of course, instantiating this array. Here are some of my attempts:

public HashTable(int size) {
  table = new LinkedList<T>[size];
}

This throws a compile-time generic array creation error.

public HashTable(int size) {
  table = (LinkedList<T>[])(new Object[size]);
}

That causes a ClassCastException error at runtime (java.lang.Object cannot be cast to LinkedList).

The person heading the project is also unsure of how to deal with this issue. Is there any way I can change my code so that the hash table still has an array as its underlying data structure with the collisions being placed in a LinkedList?

  • 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-05-26T17:01:14+00:00Added an answer on May 26, 2026 at 5:01 pm

    This worked for me:

    public class HashTable<T> {
    
        private LinkedList<T> table[];
    
        @SuppressWarnings("unchecked")
        public HashTable(int size) {
            table = new LinkedList[size];
        }
    
    }
    

    For example:

    HashTable<String> t = new HashTable<String>(10);
    t.table[0] = new LinkedList<String>();
    t.table[0].add("test");
    System.out.println(t.table[0].get(0));
    

    Yes, the constructor generated a warning (that explains the “unchecked” annotation), but afterwards the code works without more warnings.

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

Sidebar

Related Questions

Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: Get generic type of java.util.List I have a Map and I want
Possible Duplicate: Java, GUI builder or hand coding? I have been making GUI hand-coded
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: Byte array in objective-c Am converting some Java code to Objective-C and
Possible Duplicate: How to determine the class of a generic type? I have a
Possible Duplicate: Java: how to clone ArrayList but also clone its items? I have
Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: Cannot create an array of LinkedLists in Java…? I want to call
Possible Duplicate: Java - Creating an array of methods In java can i store

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.