Java中的字符串到未知数字类型(String to Unknown number type in Java)

编程入门 行业动态 更新时间:2024-10-24 21:36:25
Java中的字符串到未知数字类型(String to Unknown number type in Java)

我正在尝试创建一个通用的二叉搜索树类,它从文件中读取数字并构建BST。 我将所有单独的数字放入一个数组中,但我仍然坚持如何将它们转换为未指定的类型。

class ModifiedBinarySearchTree <N extends Number & Comprable<N>>{ private BinaryNode<N> root; public void treeInput(String fileName) throws Exception{ BufferedReader br = new BufferedReader( new FileReader(fileName)); String[] nums = br.readLine().split("\\s"); for(String num : nums){ //do something to cast number as type n //I tried: N number = N.valueOf(num); insert(number); //but that gave me a compile error }

有没有办法让这项工作? 我尝试将字符串转换为double,但后来我无法从double变为n。

I am trying to make a generic binary search tree class that reads numbers from a file and builds a BST. I got all the separate numbers into an array, but I am stuck on how to convert them to an unspecified type.

class ModifiedBinarySearchTree <N extends Number & Comprable<N>>{ private BinaryNode<N> root; public void treeInput(String fileName) throws Exception{ BufferedReader br = new BufferedReader( new FileReader(fileName)); String[] nums = br.readLine().split("\\s"); for(String num : nums){ //do something to cast number as type n //I tried: N number = N.valueOf(num); insert(number); //but that gave me a compile error }

Is there a way to make this work? I tried just converting the string to a double, but then I had no way to get from double to n.

最满意答案

所有Number类都有一个String构造函数,因此您可以安全地使用反射来调用该构造函数。 由于运行时类型擦除 ,在您的方法中无法访问类型N ,因此您必须将具体的类标记传递给构造函数,以获取对类型类的构造函数的引用:

class ModifiedBinarySearchTree <N extends Number & Comparable<N>>{ private BinaryNode<N> root; private final Constructor<N> constructor; // pass in a Number class, eg new ModifiedBinarySearchTree(Integer.class) public ModifiedBinarySearchTree(Class<N> clazz) { try { constructor = clazz.getConstructor(String.class); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } public void treeInput(String fileName) throws Exception { BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while ((line = br.readLine()) != null) { for (String num : line.split("\\s")) { insert(constructor.newInstance(num)); } } }

或者,您可以使用方法而不是构造函数传递类对象,但这会在某种程度上打败您的类的“通用性”。

我还删除了拼写错误,缩写了一些代码并添加了一个似乎缺失的while循环。

All Number classes have a String constructor, so you can safely use reflection to invoke that constructor. Due to runtime type erasure, the type N is not accessible in your method, so you must pass a concrete class token to the constructor to get a reference to the constructor for the class of the type:

class ModifiedBinarySearchTree <N extends Number & Comparable<N>>{ private BinaryNode<N> root; private final Constructor<N> constructor; // pass in a Number class, eg new ModifiedBinarySearchTree(Integer.class) public ModifiedBinarySearchTree(Class<N> clazz) { try { constructor = clazz.getConstructor(String.class); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } public void treeInput(String fileName) throws Exception { BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while ((line = br.readLine()) != null) { for (String num : line.split("\\s")) { insert(constructor.newInstance(num)); } } }

Alternatively, you could pass the class object in with the method instead of the constructor, but that would somewhat defeat the "genericness" of your class.

I also removed spelling mistakes, abbreviated some code and added a while loop that seemed to be missing.

更多推荐

本文发布于:2023-08-04 16:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   类型   数字   Java   type

发布评论

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

>www.elefans.com

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