简体   繁体   中英

Adding double value to List<double[]>

I'm making an android app, and I need to save the double value(BodySize) to a file in a form of List to draw a graph. Actually this code was in form of 'List' and I tried to change it into 'List'. But it makes error in 'list.add(BodySize)'. How can I fix this problem?

 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[]>() creates a list of arrays of doubles, not a list of doubles. The list itself is an array (of variable length), so just use :

List<Double> newList = new ArrayList<Double>(); 

Bodysize是一个double,列表是double []类型(double的数组)

List<double[]> list = getDoubles();
        list.add(BodySize);

BodySize is of type double whereas List<double[]> expects double[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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