0

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;
        }
  • 1
    The list contain double array and you are trying to add double value. Try to implement according to `Kazekage Gaara's` answer. – Shaiful Jun 21 '12 at 08:53

3 Answers3

2

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>(); 
Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • Is 'List' the same as 'double[]'? – user1453296 Jun 21 '12 at 09:02
  • An ArrayList is an array which automatically grows as you add more elements. A simple double[] array has a fixed length which cannot be changed. – Dalmas Jun 21 '12 at 09:06
  • no, the List is a list of Double Objects and the latter is an array of doubles (mind the cases) – MaVRoSCy Jun 21 '12 at 09:07
  • I have to use that values to draw a line graph using AchartEngine. In its demo code, it uses double[] type as values to draw a graph. Can I still use List in this case? – user1453296 Jun 21 '12 at 09:11
  • It is possible to convert a List to double[], read this question for more informations : http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java – Dalmas Jun 21 '12 at 09:18
  • You're welcome. But don't forget to accept the answer if it helped you (click on the check mark on the left). Please do the same for your other answers by the way. – Dalmas Jun 21 '12 at 09:42
0

Bodysize is a double, and the list is of type double [] (array of double)

Oofpez
  • 514
  • 1
  • 7
  • 18
  • I'm not familiar with types such as list, double[] and Double. What I want to do is save the double values(BodySize) in a form of array[]. What should I do? – user1453296 Jun 21 '12 at 08:57
  • Then first learn about them before implement such complex program. And also nobody knows whats in `user_data.txt`. – Shaiful Jun 21 '12 at 08:58
0
List<double[]> list = getDoubles();
        list.add(BodySize);

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

amicngh
  • 7,831
  • 3
  • 35
  • 54
  • Now I can understand what was my mistake. I want to save double values(BodySize) in a form ou double[]. Please give me some help. – user1453296 Jun 21 '12 at 09:14