簡體   English   中英

java方法檢索

[英]java method retrieves

我需要將集合發送到此方法:

public boolean[] save(T... entities) {
    return _saveOrUpdateIsNew(entities);
}

我試圖通過收藏:

List<Client> clientsToUpdate = new ArrayList<Client>();
save(clientsToUpdate );

但是我收到編譯錯誤,指出方法類型不適用於List<Client>

編輯:

添加行之后:

clientsToUpdate.toArray(new Client[0]);

我有這個編譯錯誤:

The method save(Client...) in the type BaseDAO<Client,Integer> is not applicable for the arguments (Client[])

您提到的方法使用的是varargs,這意味着它接受單個Client實例或Client對象的數組。 您應該像這樣將List轉換為數組:

List<Client> clientsToUpdate = new ArrayList<Client>();
Client[] clients = clientsToUpdate.toArray(new Client[0]);
save(clients);

除非您的項目中有多個Client類,否則這應該起作用。

T ..不是集合,而是一個數組。 因此,您必須對其進行轉換。 也許是這樣的:

for(Object o: T)
    myCollection.add(o);

編輯:

哦,對不起,我想您希望使用其他方式。 如果要將Collection傳遞給方法,請將其轉換為數組:

Object[] array = myCollection.toArray();

您不能將任何Collection傳遞給vararg方法(除非方法簽名是(Collection ...),但這幾乎肯定不是您想要的)。 嘗試使用數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM