0

Good day. I have been learning java the last few months. So I created a generic array as follows.

public class Implementation<T> implements IMatrix<T>{

    private T[][] genMatrix;
    private Integer numberRows;
    private Integer NumberCols;
    public Implementation(){
        generateMatrix();
        for(int i = 0;i< numberRows;i++)
        {
           for(int j =0;j< numberCols;j++)
           {

              JOptionPane.showInputDialog("Enter value for row " + (i+1) + " and for column " + (j+1)))

            }
        }
          multiplyScalar(5);

    }
     //generate the array
    public void generateMatrix(){
        String rowString = JOptionPane.showInputDialog("Enter the number of rows!");
        numberRows = Integer.parseInt(rowString);
        String colString = JOptionPane.showInputDialog("Enter the number of cols!");
        numberCols = Integer.parseInt(colString);

        final Object[][] arrayO = (T[][])new Object[numberRows][numberCols];
        genMatrix = (T[][])arrayO;
     }
     //writeElements to the array;
     public void writeElem(int x, int y, T value){

          genMatrix[x][y] = value;
     }
     //now that those members are done I have created a method to access the data
     public T getElem(Integer i, Integer j){
          return (T)genMatrix[i][j];
     }

This is where my problem now exists. I have made this two dimensional array. I would like to multiply each value in this array by a Integer c. I have attempted it in the following way and all failed.

 public IMatrix<T> multiplyScalar(Integer c) throws MatrixException {
    // TODO Auto-generated method stub


    for(int i = 0; i< numberRows; i++)
    {
        for(int j=0;j<numberCols;j++)
        {
            /**
                                 THIS IS THE POINT AT WHICH IT CRASHES
                             */
            System.out.println(((Integer)(getElement(i, j)) * c));


        }
    }

    return null;
} 
}

The program crashes because of a ClassCastException. I have tried everything in my knowledge to get this to work. I can not multiply the two dimensional array with a Integer. Please help. This uses a interface with many more functions that is irrelevant. Please note that there is a strong possibility that this code crashes as I can not upload the original code.

Bohemian
  • 365,064
  • 84
  • 522
  • 658
Jonathan
  • 525
  • 1
  • 8
  • 26
  • 1
    is `T` an `Integer` when you run the program? If not the cast can't work... – assylias Feb 08 '14 at 22:33
  • Explained quiet well here. http://stackoverflow.com/questions/10468853/predefining-multiplication-for-generics-in-java – user1945457 Feb 08 '14 at 22:37
  • 1
    @assylias no T is not an Integer. How can I get it a Integer – Jonathan Feb 08 '14 at 22:40
  • 1
    @Jonathan Your code as it is allows `T` to be anything, a File for example. Multiplying a File by a number does not make sense. So you need to place more constraints on what `T` can be. – assylias Feb 08 '14 at 22:41
  • If you don't need generics, don't use them! Why using an unknown type where you always expect a specific type? – bobbel Feb 08 '14 at 22:43
  • Might I suggest not naming your implementation `Implementation`? Of all the nouns you could have used... – Rainbolt Feb 08 '14 at 22:44
  • @john as i stated this is not the actual program, this is just a small part do describe the problem. – Jonathan Feb 08 '14 at 22:49

1 Answers1

1

The problem is that Java doesn't support operator polymorphism. You need T to extend Number and then use method calls. Its a bit more verbose than what one might like though. Its explained quiet well here:

Predefining multiplication for generics in java

Community
  • 1
  • 1
user1945457
  • 266
  • 3
  • 7