2017-02-21 115 views
1

如何将包含十进制数的字符串数组转换为大整数?将十进制数组转换为biginteger

如:

String s={"1","2","30","1234567846678943"}; 

我当前的代码:

Scanner in = new Scanner(System.in); 
int n = in.nextInt(); 
String s[]= new String[n]; 

for(int i=0; i < n; i++){ 
s[i] = in.next(); 
} 

BigInteger[] b = new BigInteger[n]; 
for (int i = 0; i < n; i++) { 
    b[i] = new BigInteger(String s(i)); 
} 
+0

你并不需要填充一个“字符串”数组并将其稍后转换为一个BigInteger数组。你可以用一个for循环来完成。 'BigInteger [] b = new BigInteger [n]; for(int i = 0; i

回答

1

这里:

b[i] = new BigInteger(String s(i)); 

应该是:

b[i] = new BigInteger(s[i]); 

换句话说:你的语法的一半是正确的;但随后似乎忘记了如何读取已定义的数组插槽:

  • 您使用[索引]方括号(“()”仅用于方法调用)
  • 没有必要指定“字符串”那表情
0

中键入只要使用new BigInteger(s[i]);代替new BigInteger(String s(i));

仅供参考,你真的没有使用单独的字符串数组来存储初始值。您可以直接将它们存储在BigInteger阵列中。有点像这样:

Scanner in = new Scanner(System.in); 
int n = in.nextInt(); 

BigInteger[] b = new BigInteger[n]; 

for(int i=0; i < n; i++){ 
    b[i] = new BigInteger(in.next()); 
} 
相关问题