如何转换ArrayList <double>到double []

时间:2015-07-30 17:06:14

标签: java arraylist

有谁知道如何将双arraylist传递给另一种方法? (我已经突出显示了它)我从编译器收到此消息:无法从ArrayList转换为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);
        }

4 个答案:

答案 0 :(得分:3)

您可以使用与Java8

相同的方式使用int[]
ArrayList<Double> value  = new ArrayList<Double>();
double[] arr = value.stream().mapToDouble(v -> v.doubleValue()).toArray();

(根据yshavit的评论

double[] arr = value.stream().mapToDouble(Double::doubleValue).toArray();

答案 1 :(得分:1)

您可以通过一次复制一个元素将所有元素复制到nth-of-type,但您不需要。

double[]

打印

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());

答案 2 :(得分:0)

如果您可以更改为<form action="/teams/16/import_players" method="POST" id="CsvForm" enctype="multipart/form-data"><input type="hidden" name="csrfmiddlewaretoken" value="E2lRbOD6hRQixJBNfsDL3Wamo4VzVnDj"> <div class="modal-body"> <p><label for="id_csv_file">Upload CSV File:</label> <input class="form-control input-lg" id="id_csv_file" name="csv_file" placeholder="First Name *" required="true" type="file"></p> </div> <div class="modal-footer "> <button type="submit" class="btn btn-warning btn-lg" style="width: 100%;"> Upload </button> </div> </form> ,那么您可以这样做: -

Double[]

如果你想要Double[] doubleArr= value.toArray(new Double[value.size()]); ,那么你可以这样做: -

double[]

答案 3 :(得分:0)

这是一个有效的解决方案:

[Equipment 91]
ID=EqpEmblem1
NameDisplayable= Real men need no emblem
FunctionalType= EqFTypeCoating
EquipmentInterval= NULL, NULL
EquipmentSlotType=NULL
ExternalLinkName3D= NULL
Hitpoints= 10000
DamageDescription1= NULL,   0,  1,  0,  1,  1,  invulnerable,       0,  0,  NULL,   0,  1,  1

[Equipment 92]
ID=EqpEmblem2
NameDisplayable= U-1164
FunctionalType= EqFTypeCoating
EquipmentInterval= NULL, NULL
EquipmentSlotType=NULL
ExternalLinkName3D=data\Textures\TNormal\tex\U-1164.dds
Hitpoints= 10000
DamageDescription1= NULL,   0,  1,  0,  1,  1,  invulnerable,       0,  0,  NULL,   0,  1,  1