Parse String into Number

前端 未结 6 1693
迷失自我
迷失自我 2020-12-31 04:51

I have a list which will store Number objects. The list will be populated by parsing a list of strings, where each string may represent any subclass of Number.

How d

6条回答
  •  Happy的楠姐
    2020-12-31 05:16

    Number cannot be instantiated because it is an abstract class. I would recommend passing in Numbers, but if you are set on Strings you can parse them using any of the subclasses,

    Number num = Integer.parseInt(myString);
    

    or

    Number num = NumberFormat.getInstance().parse(myNumber);
    

    @See NumberFormat

提交回复
热议问题