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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:52:50+00:00 2026-05-27T11:52:50+00:00

Possible Duplicate: Java Generics: Array containing generics I have a Java class which contains

  • 0

Possible Duplicate:
Java Generics: Array containing generics

I have a Java class which contains 2 methods which add and remove an element from an array. To make it generic it takes a subtype so it should be able to work on different types of objects.

The problem is that when I instantiate it using MapEntry (where MapEntry is an implementation of java.util.Map.Entry) as the subtype. This results in a ClassCastException being thrown when trying to convert an Object array to a MapEntry array. I’m guessing this is because of the following lines (Where T is the subtype):

array = (T[])(new Object[array.length + 1]);
array = (T[])(new Object[array.length - 1]);

Which are used to increase/decrease the array size by 1 respectively. I also use this on arrays of Integers, Strings and Objects, and it works fine with those.

Also, it’s explicitly stated that I need to use arrays for this, so no lists, etc.

Is there any way to get around this problem while still keeping the class as generic as possible?

Edit: Managed to get the problem solved. Here’s the working code:

array = (T[])Array.newInstance(array.getClass().getComponentType(), array.length + 1);
array = (T[])Array.newInstance(array.getClass().getComponentType(), array.length - 1);

Thanks for all the help 😀

  • 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-27T11:52:51+00:00Added an answer on May 27, 2026 at 11:52 am

    A) don’t use arrays, they are awful. Use collections instead.

    B) you can’t create a generic array without knowing the array type. if you do have the type (the class), you can do:

    T[] array = Array.newInstance(type, length);
    

    Read:

    • Array.newInstance(class, length)
    • How to create a generic array in Java?
    • 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 Error generic array creation I have
Possible Duplicate: Java - Creating an array of methods In java can i store
Possible Duplicate: Java generics and array initialization How does one instantiate an array of
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: When do you use Java’s @Override annotation and why? From the javadoc
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: How do you find all subclasses of a given class in Java?
Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: Java Generics To be more specific, whats the role of the <String>
Possible Duplicate: Java inner class and static nested class What are the uses of

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.