我正在尝试创建一个通用的二叉搜索树类,它从文件中读取数字并构建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.
更多推荐
发布评论