0

I'd like to create a class Matrix that accepts objects of type T extends Item for a game inventory. However, I'm having issues with implementing the class using generics / wildcards. This is what I have so far.

public class Matrix<T> {
    private final int maxVertical;
    private final int maxHorizontal;

    private T[][] contents;


    public <T extends Item> Matrix(int maxVertical, int maxHorizontal) {
         this.maxVertical = maxVertical;
         this.maxHorizontal = maxHorizontal;

         contents = new T[maxVertical][maxHorizontal];
    }
flying_loaf_3
  • 397
  • 2
  • 3
  • 12
  • "I'm having issues" What issues? – Sweeper Oct 18 '20 at 08:17
  • Syntax issues in the IDE, specifically in the `contents` initialisation (last line of constructor) – flying_loaf_3 Oct 18 '20 at 08:24
  • Is there a way to create a constructor for a generic class without having an instance of the generic as a parameter as is shown here: https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java – flying_loaf_3 Oct 18 '20 at 09:06
  • Did you look at _all_ the answers in the dupe? Look at the [answer](https://stackoverflow.com/a/2924453/5133585) by dimo414, and its comment section too. – Sweeper Oct 18 '20 at 09:10

0 Answers0