繁体   English   中英

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

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

我不明白自己在做什么错,因为出现错误:“无法实例化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    
}

破坏代码的部分是公共静态<BigInteger>类型参数...但是我不知道为什么。 当我删除此类型的参数时,编译错误就消失了。

从方法签名中删除孤独的<BigInteger> 该语法用于声明类型变量,在您的情况下这不是必需的。 如所写,您正在声明一个名为BigInteger的“占位符”,它代表某种未知类型。 在该方法中, BigInteger引用该未知类型(未知,无法实例化),而不是按您的意图引用java.math.BigInteger

同样,您应该重新访问方法名称:您的方法执行名称所建议的相反操作,即,它将字符串列表转换为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;
  }

您还忘记了返回变量名称后的分号。

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

这就是方法签名应该是的方式。 无法理解为什么静态方法签名后出现<BigInteger>。

暂无
暂无

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

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