[英]Adding double value to List<double[]>
我正在制作一個Android應用程序,我需要以List的形式將double值(BodySize)保存到文件中以繪制圖形。 實際上這個代碼是'List'的形式,我試着把它改成'List'。 但它在'list.add(BodySize)'中出錯。 我該如何解決這個問題?
public static void updateFile(double BodySize) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try{
List<double[]> list = getDoubles();
list.add(BodySize);
fos = new FileOutputStream("user_data.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(list);
}catch(Exception e){
e.printStackTrace();
try {
oos.close();
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static List<double[]> getDoubles() {
FileInputStream fis = null;
ObjectInputStream ois = null;
List<double[]> newList = new ArrayList<double[]>>();
try {
fis = new FileInputStream("user_data.txt");
ois = new ObjectInputStream(fis);
newList = (ArrayList<double[]>) ois.readObject();
} catch (Exception ex) {
try {
fis.close();
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return newList;
}
new ArrayList<double[]>()
創建一個雙精度數組列表,而不是雙精度數列表。 列表本身是一個數組(可變長度),所以只需使用:
List<Double> newList = new ArrayList<Double>();
Bodysize是一個double,列表是double []類型(double的數組)
List<double[]> list = getDoubles();
list.add(BodySize);
BodySize
是double
類型而List<double[]>
需要double[]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.