0

I want to make a function which can take any type of array (int, String, boolean, etc) and return an array of the same type with the rows and columns switched.

public static String[][] swap_rows_columns_str_arr(String[][] arr) {
    String[][] new_arr = new String[arr[0].length][arr.length];
    for (int col = 0; col < arr[0].length; col++) {
        for (int row = 0; row < arr.length; row++) {
            new_arr[col][row] = arr[row][col];
        }
    }
    return new_arr;
}

I've managed to get it work for singular datatypes, but how would I get it to work for any datatype?

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42

1 Answers1

1

You can use a generic type <T>, but you have to use Array.newInstance() to create the array (see What's the reason I can't create generic array types in Java?). The source code can look like this:

public static <T> T[][] swap_rows_columns_str_arr(T[][] arr) {
    @SuppressWarnings("unchecked")
    T[][] new_arr = (T[][]) Array.newInstance(
            arr.getClass().getComponentType().getComponentType(),
            arr[0].length, 
            arr.length);
    for (int col = 0; col < arr[0].length; col++) {
        for (int row = 0; row < arr.length; row++) {
            new_arr[col][row] = arr[row][col];
        }
    }
    return new_arr;
}

This only works for non-primitive types, as primitive types cannot be used in generics. For the primitive types you have to create eight overloads of this method.

Progman
  • 16,827
  • 6
  • 33
  • 48