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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:38:59+00:00 2026-05-14T07:38:59+00:00

public class MaxHeap<T extends Comparable<T>> implements Heap<T>{ private T[] heap; private int lastIndex; public

  • 0
public class MaxHeap<T extends Comparable<T>> implements Heap<T>{
private T[] heap;
private int lastIndex;

public void main(String[] args){
    int i;
    T[] arr = {1,3,4,5,2}; //ERROR HERE *******
    foo
}

public T[] Heapsort(T[]anArray, int n){
    // build initial heap
    T[]sortedArray = anArray;
    for (int i = n-1; i< 0; i--){
        //assert: the tree rooted at index is a semiheap
        heapRebuild(anArray, i, n);
        //assert: the tree rooted at index is a heap
    }
    //sort the heap array
    int last = n-1;
    //invariant: Array[0..last] is a heap,
    //Array[last+1..n-1] is sorted
    for (int j=1; j<n-1;j++) {
        sortedArray[0]=sortedArray[last];
        last--;
        heapRebuild(anArray, 0, last);
    }
    return sortedArray;
}

protected void heapRebuild(T[ ] items, int root, int size){
foo
}

}

The error is on the line with “T[arr] = {1,3,4,5,2}“

Eclipse complains that there is a:

“Type mismatch: cannot convert from
int to T”

I’ve tried to casting nearly everywhere but to no avail.A simple way out would be to not use generics but instead just ints but that’s sadly not an option. I’ve got to find a way to resolve the array of ints {1,3,4,5,2} into an array of T so that the rest of my code will work smoothly.

  • 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-14T07:39:00+00:00Added an answer on May 14, 2026 at 7:39 am

    When you use a generic type, you must resolve all the type parameters, i.e. tell the compiler which concrete types you want to use instead of the placeholder T in your code. As the others already pointed out, a primitive type like int can’t be used as a generic type parameter – it must be a reference type, like Integer. So you can rewrite your main method into something like

    public static void main(String[] args){
        int i = 5;
        Integer[] arr = {1,3,4,5,2};
        MaxHeap<Integer> maxHeap = new MaxHeap<Integer>();
    
        maxHeap.heapSort(arr, i);
    }
    

    Note that it should be static. When you instantiate your class, you have to specify the type parameter Integer as above. Then you can pass it the array to be sorted.

    A further note: this loop

    for (int i = n-1; i< 0; i--){
        ...
    }
    

    will never execute – the loop condition should be i > 0 instead.

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

Sidebar

Related Questions

public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public class Main { public static void main (String args[]) { int nums[]= {2,
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker
public class Demo { public static void main(String args[]) { double d = 12.345;
public class SaveData extends Activity implements OnClickListener { private DataManipulator dh; static final int
public class MCQSample extends Activity implements OnClickListener{ TextView title; String gotBread; RadioGroup AnswerRG; int
public class HelloWorld { public static void main (String args[]){ System.out.println (Hello ); http://www.google.com
public class Test { public static void main(String[] args) throws Exception { String s1
public class Test { public static void main(String[] args) { } } class Outer

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.