在矩阵加法/乘法中使用泛型(Using Generics in Matrix Addition/Multiplication)

编程入门 行业动态 更新时间:2024-10-27 10:25:55
在矩阵加法/乘法中使用泛型(Using Generics in Matrix Addition/Multiplication)

我正在尝试创建一个自定义类,创建矩阵,除其他外,执行添加所有单元格或将它们全部相乘的操作。 但是,我想使用泛型,因此矩阵可以是任何类型的数字:float,double,int等。因此我设置了这样的类:

public class Matrix<num>

初始化时,此类的实例化会根据用户提供的数据创建一个矩阵,该矩阵存储在实例的.matrix变量中。 现在,在我想要添加所有单元格的代码中,我执行以下操作:

public num addMatrices(num[][] toAdd){ num result; if (toAdd.length != this.rows && toAdd[0].length != this.columns){ System.out.println("Matrix mismatch. Try Again."); return toAdd[0][0]; } for (int i=0; i<rows; i++) for (int j=0; j<rows; j++){ result = this.matrix[i][j] + toAdd[i][j]; } }

然而,我遇到了多个问题。 首先,我无法将结果初始化为零,这使得执行+ =操作变得困难。 其次,当我尝试添加两个矩阵的单元格时,编译器告诉我+类型为num的+运算符。

我认为泛型的整个要点是有一个catchall类型,所以我可以做一些事情,比如在一个案例中使用浮点数,在另一个案例中使用ints,但如果我需要为像+这样的运算符指定类型,我不确定它的优势在哪里进来...

I'm trying to create a custom class the creates matrices and, among other things, performs operations that add up all of the cells or multiply them all. However, I want to use generics so the matrices can be any type of number: float, double, int, etc. I have thus set up the class like this:

public class Matrix<num>

Upon initialization, the instantiation of this class creates a matrix based on user supplied data, stored in the instance's .matrix variable. Now, in the code where I want to add up all of the cells, I do something like this:

public num addMatrices(num[][] toAdd){ num result; if (toAdd.length != this.rows && toAdd[0].length != this.columns){ System.out.println("Matrix mismatch. Try Again."); return toAdd[0][0]; } for (int i=0; i<rows; i++) for (int j=0; j<rows; j++){ result = this.matrix[i][j] + toAdd[i][j]; } }

I'm running into multiple problems, however. First of all, I can't initialize result to zero, which makes it hard to perform += operations. Secondly, when I try to add the cells of the two matrices, the compiler tells me that the + operator is undefined for the type num.

I thought the whole point of generics was to have a catchall type so I could do things like use floats in one case and ints in another, but if I need to specify the type for operators like +, I'm not sure where the advantage comes in...

最满意答案

您无法对对象执行+和-等操作(某些特殊情况除外)。 泛型都是关于对象类型的,所以你的用例并不理想。

也就是说,您可以将声明转换为public class Matrix<num extends Number> ,这将允许您传入Integer , Double , BigInteger等,然后您可以使用num.longValue()或num.doubleValue()获得数字的长或双表示。 然后,您需要返回一个double或long或者您的方法而不是您的泛型类型。

另一种选择是创建一个自定义容器类,它具有add,subtract等方法。然后你的类声明可以是public class Matrix<num extends Custom> 。 您必须弄清楚如何将long添加到双精度并返回Custom类型。

You can't perform operations like + and - on Objects (some special cases excluded). Generics are all about Object types, so your use case for them isn't ideal.

That said, you can turn your declaration into something like public class Matrix<num extends Number>, which will let you pass in Integer, Double, BigInteger, etc. then you can then use something like num.longValue() or num.doubleValue() to get the long or double representations of your numbers. You would then need to return a double or long or whatever from your method instead of your generic type, though.

the other option would be to create a custom container class that has methods for add, subtract, etc. Then your class declaration can be public class Matrix<num extends Custom>. You would have to figure out how to account for adding longs to doubles and returning the Custom type.

更多推荐

本文发布于:2023-07-04 06:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1019753.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加法   乘法   矩阵   Generics   Addition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!