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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:41:48+00:00 2026-05-27T13:41:48+00:00

I’ve to solve a problem regarding generics which gives me quite the stomache ache.

  • 0

I’ve to solve a problem regarding generics which gives me quite the stomache ache.

Basically it’s supposed to be an implementation for an array-based max-heap and I have to follow the given conditions.
The code to start from looks like this:

//Interface with methods such as insert(), remove(), etc
public interface IMaxHeap<T extends Comparable<T>> {
   ...
}

public class MaxHeap<T extends Comparable<T>> implements IMaxHeap<T>{
    T[] m_array;

    ...

    public MaxHeap(int size){
        //TODO:
        //m_array = new T[size];
    }
    public MaxHeap(T[] array){
        //TODO:
        //Deep copy of array into m_array
    }
}

The code for the two constructors is what I’m strugling with.
I am not supposed to modify the signature of the methods so I can’t do things like this:

public MaxHeap(Class<T> clazz, int size){
    m_array = (T[]) Array.newInstance(clazz, size);
}

Additionally, doing the “unsafe” type-casting of an Object[] array like this gives me a runtime excetion (InvocationTargetException)

public MaxHeap(Class<T> clazz, int size){
    m_array = (T[]) new Object[size];
}

I’ve found a generic way to make a copy of my array, however it’s not a deep copy:

public MaxHeap(T[] array){
    m_array = Arrays.copyOf(array, array.length);
}

So that this point m_array[0] == array[0] which isn’t what I’m looking for.

I’m also not supposed to add other constraints to the class (so no T exdends Cloneable either). Given these problems I haven’t actually found a way to instantiate my arrays.

I’m somewhat new to generics (I’ve only been working with C++ templates) so those quirks with T are pretty annoying.
Can you guys point me to a solution that works?

  • 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-27T13:41:48+00:00Added an answer on May 27, 2026 at 1:41 pm

    From my experience, the standard thing to do is to make the backing array be of type Object[], then cast to type T when reading its elements.

    Generally speaking, working with generic-typed arrays in Java is really dangerous and confusing. It’s best to avoid it as much as possible.

    But as you insist on doing things this way, this seems to be the solution:

        public MaxHeap(int size){
            m_array = (T[])new Comparable[size];
        }
        public MaxHeap(T[] array){
            this(array.length);
            System.arraycopy(array, 0, m_array, 0, array.length);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a text area in my form which accepts all possible characters from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.