in Education by
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> { ... } public class MaxHeap> implements IMaxHeap{ 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 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 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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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); }

Related questions

0 votes
    I've to solve a problem regarding generics which gives me quite the stomache ache. Basically it's supposed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I want to transfer the contents from part of the a 'Type' array to an excel range (see example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Here is what I want to do: protocol GenericFactory { associatedtype Input associatedtype Value func create(with ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic class to return and accept any type of object? (a) K ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic class to return and accept a number? (a) K (b) N ... Interfaces & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept any type of object? (a) ... chapter Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept a number? (a) K (b) N ... in portion Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these types cannot be used to initiate a generic type? (a) Integer class (b) Float class (c) ... in section Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these types cannot be used to initiate a generic type? (a) Integer class (b) Float class ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...