Java Generics and numbers

后端 未结 11 1209
离开以前
离开以前 2021-02-05 08:58

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 <         


        
11条回答
  •  遇见更好的自我
    2021-02-05 09:41

    I think you are fighting a losing battle. Even if you solve this, how are you planning on solving addition, subtraction etc? The number class is not a very useful superclass, and about the only useful method is doubleValue().

    Zero can be defined as the identity in addition or a zero in multiplication, but without a generic definition of addition or multiplication, a generic definition of zero is unlikely.

    If you want this then you might be better just sticking with BigDecimal for everything, but of course that will have associated performance penalties.

    The other obvious option would be to leave the array with null initialisation, and then change your other code to treat null as zero.

提交回复
热议问题