-2

Does anyone know how to pass a double arraylist into another method? (I have highlighted it)I get this message from compiler : cannot convert from ArrayList to double[]

                ArrayList<Double> value  = new ArrayList<Double>();
                while (rs.next()) 
                {  
                    ArrayList<Integer> r=new ArrayList<Integer>(); 
                    r.add(rs.getInt("Type"));
                    r.add(rs.getInt("Budget"));
                    r.add(rs.getInt("Day"));
                    r.add(rs.getInt("Preferences"));
                    int vec2[] = r.stream().mapToInt(t -> t).toArray();
                    double cos_sim=cosine_similarity(vec1,vec2);
                    value.add(cos_sim);
                }

                pick_highest_value_here_and_display(value);
                ps.close();
                rs.close();
                conn.close();


            }


    private void pick_highest_value_here_and_display(ArrayList<Double> value) {
            // TODO Auto-generated method stub
            **double aa[]=value ;**
            double highest=aa[0];
            for(int i=0;i<aa.length;i++)
            {
                if(aa[i]>highest){
                    highest=aa[i];
                }

            }

            System.out.println(highest);
        }
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • @Jashaszun does double method same with int? – John Joe Jul 30 '15 at 17:08
  • 2
    Apart of the fact that is possible duplicate as already stated from someone why exactly you want to make it array? You can get elements of ArrayList by index too also to modify them by index too. – ap0calypt1c Jul 30 '15 at 17:09
  • 1
    Your `pick_highest_value_here_and_display` method could simply be `double maxValue = Collections.max(...);` – Alexis C. Jul 30 '15 at 17:12

4 Answers4

4

You can use Java8 in a same way you used it for int[]

ArrayList<Double> value  = new ArrayList<Double>();
double[] arr = value.stream().mapToDouble(v -> v.doubleValue()).toArray();

OR (As per below comment of yshavit)

double[] arr = value.stream().mapToDouble(Double::doubleValue).toArray();
Community
  • 1
  • 1
akash
  • 22,664
  • 11
  • 59
  • 87
1

You can copy all the element to a double[] by copying one at a time, but you don't need to.

List<Double> value = Arrays.asList(1.1, 3.3, 2.2);

Optional<Double> max = value.stream().max(Comparator.<Double>naturalOrder());
System.out.println(max.get());

prints

3.3
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

If you can change to Double[] then you can do like :-

Double[] doubleArr=  value.toArray(new Double[value.size()]);

Else if you want double[], then you can do like :-

double[] doubleArr= new double[value.size()];
    int index = 0;
    for(double i : value){
        doubleArr[index] = i; // unboxing is automtically done here
        index++;
    } 
AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
0

Here is a working solution:

        ArrayList<Double> a = new ArrayList<Double>();
        a.add(0.23);
        a.add(2.4);
        Double[] b = new Double[a.size()];
        b = a.toArray(b);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }
rhitz
  • 1,892
  • 2
  • 21
  • 26