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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:45:16+00:00 2026-06-13T07:45:16+00:00

I am trying to create an array of the generic class DataStruct. The code

  • 0

I am trying to create an array of the generic class “DataStruct”.
The code is the following:

public class DataArray<T> {
DataStruct<T>[] array;
int index;

public DataArray(int capacity) {
    array = (DataStruct<T>[]) new Object[capacity]; // !!!
    this.index = 0;
}
}

I get a java.lang.ClassCastException (Ljava.lang.Object; cannot be cast to [LArrayBased.DataStruct;) at the line marked with three exclamation marksat the end, while testing it.

Can you please tell me the correct way to create it?

  • 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-13T07:45:18+00:00Added an answer on June 13, 2026 at 7:45 am

    Why not declare

    array = new DataStruct[capacity];
    

    Object[] can not be cast to DataStruct[].

    Because arrays are refiable in nature that means arrays know their type at runtime so If you convert it to Object [] like below you will again run in to problems

    Object[] array = new DataStruct[capacity]; 
    array[0] = 10;//Array Store exception
    

    So it is wise to declare it as DataStruct[capacity]

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

Sidebar

Related Questions

I am trying to create an array of class objects taking an integer argument.
I'm new to ASP.NET, and I'm trying to create a class that will call
I'm basically trying to create a generic object in javascript that I can use
im trying to create an array: int HR[32487834]; doesn't this only take up about
I am trying to create a thread in the following code but the pointer
I'm trying to specify that a generic class has to be an array, or
I am trying to make a generic method: class Foo attr_reader def add(object) item
I'm trying to figure out how to create a new object template at runtime
Im trying to create an array of tm* structs, and then return them at
I am trying to create an array of product SKU's which I can then

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.