有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将double[]转换为long[]

正如标题所说,我必须将double[]转换为long[],但我不知道该怎么做

我试试这个:

double[] ids = getIds(); // The ids I'm getting return in double format
List<Long> myArray = new ArrayList<Long>();

for (double _ids : ids) {
    myArray.add((long)_ids);
}

System.out.println(myArray); // Works! the list is now long, I can print also in myArray.toArray()

// Now I need pass this on a long[] array for the system wich request the data
// Receive "getStringsById(String string, long... Ids)"

我真的很感谢你的帮助

谢谢你抽出时间


共 (1) 个答案

  1. # 1 楼答案

    创建一个long[],而不是创建一个List<Long>,后者需要调用toArray()来获取一个long[]

    long[] myArray = new long[ids.length];
    int i = 0;
    for (double _ids : ids){
        myArray[i++] = (long) _ids;
    }