Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BigDecimal的类型转换_Java_String_Casting_Arraylist_Bigdecimal - Fatal编程技术网

Java BigDecimal的类型转换

Java BigDecimal的类型转换,java,string,casting,arraylist,bigdecimal,Java,String,Casting,Arraylist,Bigdecimal,我有一个方法返回类型为ArrayList,它也在读取相同类型的参数。现在,我如何输入cast或修改我的BigDecimal来读取该值 public static ArrayList<String> currencyUtilArray(ArrayList<String> amountStr ) { BigDecimal amount = new BigDecimal(ArrayList<String> amountStr); //How do I define

我有一个方法返回类型为
ArrayList
,它也在读取相同类型的参数。现在,我如何
输入cast
或修改我的BigDecimal来读取该值

public static ArrayList<String> currencyUtilArray(ArrayList<String> amountStr ) {
BigDecimal amount = new BigDecimal(ArrayList<String> amountStr);
//How do I define that the amountStr is ArrayList<String> and need to place in BigDecimal amount one after another till the end of the List 
    return amountStr;
}
publicstaticarraylistcurrencyutilarray(arraylistamountstr){
BigDecimal金额=新的BigDecimal(ArrayList amountStr);
//我如何定义amountStr是ArrayList,并且需要一个接一个地以BigDecimal的形式放置,直到列表的末尾
返回金额str;
}

或者我需要做什么?

我认为您引用的内容不正确。不能将整个ArrayList作为字符串类型的AL转换为大小数

首先,将ArrayList amountStr从新的BigDecimal(--)中更改;并引用ArrayList中的单个字符串

这意味着您必须循环整个过程,将其添加到金额中:

BigDecimal amount = new BigDecimal(amountStr.get(0));
For(int i = 1; i < amountStr.size(); i++){
amount = amount.add(new BigDecimal(amountStr.get(i)));
}
BigDecimal金额=新的BigDecimal(amountStr.get(0));
对于(int i=1;i

我相信当它返回时,它会给你你所需要的。

你不能将
字符串
类型转换为
大十进制
,反之亦然

如果数组中的字符串在连接时表示一个数字,请执行以下操作:

 StringBuilder sb = new StringBuilder();
 for (String s : amountStr) {
     sb.append(s);
 }
 BigDecimal amount = new BigDecimal(sb.toString());
另一方面,如果每个字符串代表一个不同的数字:

for (String s : amountStr) {
    BigDecimal amount = new BigDecimal(s);
    // ... and do something with it ...
}

我的理解是,您希望将
列表元素
转换为
大十进制数组
。如果是这样,您可以执行以下操作:

BigDecimal[] amount = new BigDecimal[amountStr.size()];
for (int i=0;i<amountStr.size();i++) {
     amount[i] = new BigDecimal(amountStr.get(i));
 }
BigDecimal[]amount=新的BigDecimal[amountStr.size()];

对于(int i=0;我正在尝试将
ArrayList
转换为
ArrayList
?@Gabe一个方法将调用并发送值列表,现在说两个值,比如
1234.5678
4567.1234
到这个方法
currUtil
,现在我需要定义我的方法
currUtil
,这样它可以读取一个值。)d将它一个接一个地放在BigDecimal中进行处理。如何定义我的方法?我实际上对这个问题很困惑。冗长的文字让我很难准确地指出你在问什么。如果你试图创建一个ArrayList,那么就使用我的代码,但更改for循环之间的内容,因为这不会给你其他可能的结果如果是这样的话。