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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:44:50+00:00 2026-06-10T19:44:50+00:00

I have a problem with instantiating a generic type array, here is my code:

  • 0

I have a problem with instantiating a generic type array, here is my code:

public final class MatrixOperations<T extends Number>
{
    /**
 * <p>This method gets the transpose of any matrix passed in to it as argument</p>
 * @param matrix This is the matrix to be transposed
 * @param rows  The number of rows in this matrix
 * @param cols  The number of columns in this matrix
 * @return The transpose of the matrix
 */
public T[][] getTranspose(T[][] matrix, int rows, int cols)
{
    T[][] transpose = new T[rows][cols];//Error: generic array creation
    for(int x = 0; x < cols; x++)
    {
        for(int y = 0; y < rows; y++)
        {
            transpose[x][y] = matrix[y][x];
        }
    }
    return transpose;
}
}

I just want this method to be able to transpose a matrix that it’s class is a subtype of Number and return the transpose of the matrix in the specified type. Anybody’s help would be highly appreciated. Thanks.

  • 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-10T19:44:52+00:00Added an answer on June 10, 2026 at 7:44 pm

    You can use java.lang.reflect.Array to dynamically instantiate an Array of a given type. You just have to pass in the Class object of that desired type, something like this:

    public T[][] getTranspose(Class<T> arrayType, T[][] matrix, int rows, int cols)
    {
    
        T[][] transpose = (T[][]) Array.newInstance(arrayType, rows,cols);
        for (int x = 0; x < cols; x++)
        {
            for (int y = 0; y < rows; y++)
            {
                transpose[x][y] = matrix[y][x];
            }
        }
        return transpose;
    }
    
    public static void main(String args[]) {
        MatrixOperations<Integer> mo = new MatrixOperations<>();
        Integer[][] i = mo.getTranspose(Integer.class, new Integer[2][2], 2, 2);
        i[1][1] = new Integer(13);  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello, I have the following problem: public class TestCombo extends JFrame{ public TestCombo() {
I have this problem: After instantiating this Window, open all nested expanders on the
I have a Generic class that takes an object of type T, serializes it
I have a problem I need to solve regarding my Javascript code. When instantiating
I have problem with my query on C, I’m using the oci8 driver. This
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
I have posted a question on here previously asking similar advise, but this project
I have a JS class inside which I am instantiating a goog.net.XhrIo object. Now
I'm slowly learning C# and bit confused about the code. I have here a
I have some code for you guru's to check. I know this is probably

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.