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

  • SEARCH
  • Home
  • 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 7875491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:59:25+00:00 2026-06-03T02:59:25+00:00

Possible Duplicate: Java how to: Generic Array creation Error: Generic Array Creation I am

  • 0

Possible Duplicate:
Java how to: Generic Array creation
Error: Generic Array Creation

I am getting this error:

Cannot create a generic array of T

This is my code (error on line 6):

1    public class HashTable<T> {
2    
3        private T[] array;
4    
5        HashTable(int initSize) {
6            this.array = new T[initSize];
7        }
8    }

I am wondering why this error is appearing and the best solution to fix it. Thanks.

UPDATE:

I adjusted my code so that the array is taking in linked lists instead, but I am getting a new error.

Here is my error:

Cannot create a generic array of LinkedList<T>

Here is my code (error on line six):

1    public class HashTable<T> {
2    
3        private LinkedList<T>[] array;
4    
5        HashTable(int initSize) {
6            this.array = new LinkedList<T>[initSize];
7        }
8    }

Is this error for the exact same reason? I was just assuming I could create generic linked lists and just store them in the array.

  • 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-03T02:59:26+00:00Added an answer on June 3, 2026 at 2:59 am

    Generic arrays can be created via reflection (although an unsafe cast is required), you just need to pass the class as a parameter (assuming that the following method is inside a class that defines a <T> type parameter):

    @SuppressWarnings("unchecked")
    public T[] createArray(Class<T> klass, int size) {
        return (T[]) Array.newInstance(klass, size);
    }
    

    For example, in your case:

    HashTable<Integer> t = new HashTable<Integer>();
    Integer[] intArray = t.createArray(Integer.class, 4);
    intArray[0] = 1; intArray[1] = 2;
    intArray[2] = 3; intArray[3] = 4;
    
    System.out.println(Arrays.toString(intArray));
    > [1, 2, 3, 4]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java how to: Generic Array creation import java.util.EmptyStackException; import java.util.Vector; public class
Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have
Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: Why won't this generic java code compile? Given the following code: import
Possible Duplicate: Cannot create an array of LinkedLists in Java…? I want to call
Possible Duplicate: java String concatenation How to Improve performance of this chunk of code
Possible Duplicate: Instantiating a generic class in Java I am studying Generics Java because
Possible Duplicate: Byte array in objective-c Am converting some Java code to Objective-C and
Possible Duplicate: Java: Start Mail-Client with Attachment? I need to create a java code
Possible Duplicate: Java Generics: Array containing generics I have a Java class which contains

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.