问题描述
试图看看我是否可以清理一些数学代码,主要是矩阵的东西,我试图使用一些Java泛型。我有以下方法: private< T> T [] [] zeroMatrix(int row,int col){
T [] [] retVal =(T [] [])new Object [row] [col]; (int j = col; j< col; j ++){
retVal [i] [j] = {
for(int i = row; i }
}
return retVal;
}
retVal [i] [j] = 0是引起我的那一行头痛。该行的目标是用T表示0初始化数组。我试图用它做各种事情:(T在类中定义为T扩展数字)
retVal [i] [j] =(T)0;
retVal [i] [j] = new T(0);
唯一可行的是
retVal [i] [j] =(T)new Object(0);
这不是我想要的。
这可能吗?是否有更简单的方法来表示任何类型的数字(包括潜在的BigDecimal)的NxM矩阵,或者我卡住了?
< T extends Number> T [] [] zeroMatrix(Class< ;? extends Number> int row,int col){
T [] [] matrix =(T [] [])java.lang.reflect.Array.newInstance行,列);
T zero =(T)of.getConstructor(String.class).newInstance(0);
//不处理异常
for(int i = 0; i< row; i ++){
for(int j = 0; j< col;
矩阵[i] [j] =零;
}
}
返回矩阵;
}
用法:
BigInteger [] [] bigIntegerMatrix = zeroMatrix(BigInteger.class,3,3);
Integer [] [] integerMatrix = zeroMatrix(Integer.class,3,3);
Float [] [] floatMatrix = zeroMatrix(Float.class, 3,3);
String [] [] error = zeroMatrix(String.class,3,3); //< ---编译时错误
System.out.println(Arrays.deepToString (bigIntegerMatrix));
System.out.println(Arrays.deepToString(integerMatrix));
System.out.println(Arrays.deepToString(floatMatrix));
编辑
一个通用矩阵:
public static< T> T [] [] fillMatrix(Object fill,int row,int col){
T [ ] [] matrix =(T [] [])Array.newInstance(fill.getClass(),row,col); (int j = 0; j matrix [i])的
(int i = 0; i }
}
返回矩阵;
}
Integer [] [] zeroMatrix = fillMatrix(0,3,3); //一个零填充的3x3矩阵
String [] [] stringMatrix = fillMatrix(B,2,2); //一个B填充的2x2矩阵
In an attempt to see if I can clean up some of my math code, mostly matrix stuff, I am trying to use some Java Generics. I have the following method:
private <T> T[][] zeroMatrix(int row, int col) { T[][] retVal = (T[][])new Object[row][col]; for(int i = row; i < row; i++) { for(int j = col; j < col; j++) { retVal[i][j] = 0; } } return retVal; }
The line retVal[i][j] = 0 is the one causing me headaches. The goal of the line is to initialize the array with the T representation of 0. I've attempted to do all sorts of things with it: (T is defined in the class as T extends Number)
retVal[i][j] = (T)0; retVal[i][j] = new T(0);
The only thing that works is
retVal[i][j] = (T)new Object(0);
Which is not what I want.
Is this possible? Is there an easier way to represent an NxM matrix of any type of Number(including potentially BigDecimal), or am I stuck?
解决方案<T extends Number> T[][] zeroMatrix(Class<? extends Number> of, int row, int col) { T[][] matrix = (T[][]) java.lang.reflect.Array.newInstance(of, row, col); T zero = (T) of.getConstructor(String.class).newInstance("0"); // not handling exception for (int i = 0; i < row; i++) { for (int j = 0; j < col; matrix[i][j] = zero; } } return matrix; }
usage:
BigInteger[][] bigIntegerMatrix = zeroMatrix(BigInteger.class, 3, 3); Integer[][] integerMatrix = zeroMatrix(Integer.class, 3, 3); Float[][] floatMatrix = zeroMatrix(Float.class, 3, 3); String[][] error = zeroMatrix(String.class, 3, 3); // <--- compile time error System.out.println(Arrays.deepToString(bigIntegerMatrix)); System.out.println(Arrays.deepToString(integerMatrix)); System.out.println(Arrays.deepToString(floatMatrix));
EDIT
a generic matrix:
public static <T> T[][] fillMatrix(Object fill, int row, int col) { T[][] matrix = (T[][]) Array.newInstance(fill.getClass(), row, col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { matrix[i][j] = (T) fill; } } return matrix; } Integer[][] zeroMatrix = fillMatrix(0, 3, 3); // a zero-filled 3x3 matrix String[][] stringMatrix = fillMatrix("B", 2, 2); // a B-filled 2x2 matrix
这篇关于Java泛型和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!