【问题标题】:How to convert from ArrayList<Double> to double[] [duplicate]如何从 ArrayList<Double> 转换为 double[] [重复]
【发布时间】:2015-10-22 04:13:09
【问题描述】:

有谁知道如何将双数组列表传递给另一个方法? (我已突出显示)我从编译器收到此消息: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);
        }

【问题讨论】:

  • @Jashaszun 的 double 方法与 int 相同吗?
  • 除了可能重复的事实,正如某人已经说过的那样,您为什么要使其成为数组?您也可以通过索引获取 ArrayList 的元素,也可以通过索引修改它们。
  • 你的 pick_highest_value_here_and_display 方法可以简单地是 double maxValue = Collections.max(...);

标签: java arraylist


【解决方案1】:

您可以像使用 int[] 一样使用 Java8

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

OR(根据yshavit下面的评论)

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

【讨论】:

【解决方案2】:

如果您可以更改为Double[],那么您可以这样做:-

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

如果你想要double[],那么你可以这样做:-

double[] doubleArr= new double[value.size()];
    int index = 0;
    for(double i : value){
        doubleArr[index] = i; // unboxing is automtically done here
        index++;
    } 

【讨论】:

    【解决方案3】:

    您可以通过一次复制一个来将所有元素复制到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());
    

    打印

    3.3
    

    【讨论】:

    • 这很有用。我从未注意到max 方法,并且到目前为止一直在使用reduce 进行此类计算。
    【解决方案4】:

    这是一个可行的解决方案:

            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]);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 2015-01-01
      • 2020-06-22
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多