2014-03-19 121 views
0

就像標題所說,我必須將double[]轉換爲long[],但我不知道該怎麼做。Convert double [] to 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

你爲什麼直接創建'List '而不是''''''''? –

+3

一個'清單'不是'長[]' – nachokk

回答

5

創建一個long[]而不是創建一個List<Long>,這需要致電toArray()以獲得long[]

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

謝謝,這工作得很好! ;) – user3404207