Java 如何从列表中获取值<;双倍>;把它放进双人床里

Java 如何从列表中获取值<;双倍>;把它放进双人床里,java,android,Java,Android,我正在从Firebase获取一个数组,我想从数组中检索值(我能够成功地打印它们,但无法将它们存储到双精度文件中) 编辑: 我将列表类型从Double更改为Long,无法获得“[60]”的输出。我的Firebase Cloud Firestore中存储了值60 List<Long> weight = (List<Long>) data.get("Weight"); int weightLength = weight.size(); double curr

我正在从Firebase获取一个数组,我想从数组中检索值(我能够成功地打印它们,但无法将它们存储到双精度文件中)

编辑: 我将列表类型从Double更改为Long,无法获得“[60]”的输出。我的Firebase Cloud Firestore中存储了值60

List<Long> weight = (List<Long>) data.get("Weight");
int weightLength = weight.size();
double currentWeight = weight.get(weightLength-1);
List weight=(List)data.get(“weight”);
int weightLength=weight.size();
double currentWeight=weight.get(weightLength-1);

问题是long可以转换为double,但不能转换为long。两种解决方案:

将long保留为double到编译器,只需确保long:

double currentWeight = (long) weight.get(weightLength-1);
使用long、long、int、Integer和all都是数字,并使用转换函数:

double currentWeight = weight.get(weightLength-1).doubleValue();
后者是最好的

我看到@fatma zehra güç给出了这个错误,可能是在OP编辑之前。

data.get(“Weight”)
返回您试图强制转换到
List
List
,这是不允许的,例如,以下代码甚至无法通过编译:

List<Long> list = List.of(1L, 2L);
List<Double> weight = (List<Double>) list;
此外,您不需要将
long
显式转换为
double
,即下一行

double currentWeight = (double) weight.get(weightLength-1);
可以简单地编写,而无需显式转换为

double currentWeight = weight.get(weightLength - 1);

你绝对确定列表中的对象是双精度而不是长精度吗?这很有效,但是它给了我“[60]”而不是所需的“60”,你能解释一下原因吗?@JoshMunstermann-你得到的是
[60]
,因为
系统.out.println(重量)自动转换为
System.out.println(weight.toString()),如果查看的文档,您会发现它返回一个由
[]
括起的元素字符串。但是,
System.out.println(weight.get(weightness-1))
将为您提供
60
。如果还有任何疑问/问题,请随时发表评论。非常感谢您的帮助,我需要使用.toString(),因为我正在尝试设置text()。有什么方法可以克服这个问题吗?@JoshMunstermann-是的,你可以使用
setText(String.valueOf(weight.get(weightLength-1))
。如果要设置完整的
列表
,可以执行
setText(weight.toString().replaceAll(“[\\[\]]”,“”))
List<Long> weight = (List<Long>) data.get("Weight");
double currentWeight = (double) weight.get(weightLength-1);
double currentWeight = weight.get(weightLength - 1);