简体   繁体   English

从 Arraylist 转换<double>到原始类型双数组</double>

[英]Convert from Arraylist<Double> to primitivetype double array

Hey I am trying to get my ArrayList from DBhelper class and convert it to a double from Doublearraylist since I want to be able to calculate the value from another variable.嘿,我正在尝试从 DBhelper class 获取我的 ArrayList 并将其转换为 Doublearraylist 中的双精度值,因为我希望能够从另一个变量中计算值。

My dbhelperclass contains this code for getting the arraylist我的 dbhelperclass 包含用于获取 arraylist 的代码

public ArrayList<Double> getLastEntry(){
    ArrayList<Double> data = new ArrayList<Double>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.query(TABLE_NAME, new String[]{COL2},null, null, null, null, null);
    Double add = null;
    while(cursor.moveToNext()){
        if(cursor.moveToLast()){
            add=cursor.getDouble(0);
            data.add(add);
        }
    }
    cursor.close();
    return data;
}

And so far I tried two things to convert the data but in my error message I get found java.lang.double need a string to check if it can be logged in logcat but I just get all these symbols logged instead of a string so I guess I converted it wrongly.到目前为止,我尝试了两件事来转换数据,但在我的错误消息中我发现 java.lang.double 需要一个字符串来检查它是否可以登录到 logcat 但我只是记录了所有这些符号而不是字符串所以我猜我转换错了。

Code for the converter转换器的代码

First method i tried我试过的第一种方法

ArrayList<Double> data = mDatabaseHelper.getLastEntry();
double []arrX =  new double [data.size()];
for(int j=0; j<data.size();j++){
    double convert = data.get(j);
    arrX[j]=convert;
}
Log.d(TAG, arrX.toString());

Second method I tried was from a book with newer java i think.我尝试的第二种方法来自我认为较新的 java 的书。 but it says cannot infer arguments inbetween the new Arraylist但它说不能在新的 Arraylist 之间推断 arguments

code:代码:

data = new ArrayList<>(Arrays.asList(data));
double[] arr = data.stream().mapToDouble(Double::doubleValue).toArray();

Regarding the second way - i think there is a mistake:关于第二种方式 - 我认为有一个错误:

data = new ArrayList<>(Arrays.asList(data)); -> here you make an ArrayList<Double> into an ArrayList<ArrayList<Double>> . -> 在这里你将一个ArrayList<Double>变成一个ArrayList<ArrayList<Double>>

You probably might want to write:你可能想写:

ArrayList<Double> data = mDatabaseHelper.getLastEntry();
double[] arr = data.stream().mapToDouble(Double::doubleValue).toArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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