简体   繁体   English

如何在这样的Java静态方法中实例化BigInteger?

[英]How do I instantiate a BigInteger in a java static method like this?

I don't understand what I'm doing wrong here because I get an error: 'Cannot instantiate the type BigInteger' 我不明白自己在做什么错,因为出现错误:“无法实例化BigInteger类型”

public static <BigInteger> List<BigInteger> convertIntegerListToStringList(List<String> existingList) {
      ArrayList<BigInteger> newList = new ArrayList<BigInteger>();
      for(String item : existingList) {
        newList.add(new BigInteger(item));
      }
      return newList    
}

the part that is breaking the code is the public static <BigInteger> type parameter...but I have no idea why. 破坏代码的部分是公共静态<BigInteger>类型参数...但是我不知道为什么。 When i remove this typed parameter then the compilation error goes away. 当我删除此类型的参数时,编译错误就消失了。

Remove the lone <BigInteger> from your method signature. 从方法签名中删除孤独的<BigInteger> That syntax is for declaring type variables, which is not necessary in your case. 该语法用于声明类型变量,在您的情况下这不是必需的。 As written, you are declaring a "placeholder" called BigInteger that represents some unknown type. 如所写,您正在声明一个名为BigInteger的“占位符”,它代表某种未知类型。 Within the method, BigInteger refers to that unknown type (which, being unknown, cannot be instantiated) instead of referring to java.math.BigInteger as you intended. 在该方法中, BigInteger引用该未知类型(未知,无法实例化),而不是按您的意图引用java.math.BigInteger

Also, you should revisit your method name: your method performs the opposite operation that the name suggests, ie, it converts a string list to a BigInteger list, not the other way around. 同样,您应该重新访问方法名称:您的方法执行名称所建议的相反操作,即,它将字符串列表转换为BigInteger列表,而不是相反。

Your method signature has wrong syntax. 您的方法签名语法错误。 You should remove <BigInteger> to make function return object of type List<BigInteger> 您应该删除<BigInteger>以使函数返回类型为List<BigInteger>对象

  public static List<BigInteger> convertIntegerListToStringList(List<String> existingList)
  {
      ArrayList<BigInteger> newList = new ArrayList<BigInteger>();
      for(String item : existingList) {
        newList.add(new BigInteger(item));
      }
    return newList;
  }

You also forgot about semicolon after return variable name. 您还忘记了返回变量名称后的分号。

public static {ReturnType} {MethodName} ({Arguments})

This is how your method signature should be. 这就是方法签名应该是的方式。 Cannot understand why you are having <BigInteger> after static in method signature. 无法理解为什么静态方法签名后出现<BigInteger>。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM