简体   繁体   English

java方法检索

[英]java method retrieves

I need to send collection to this method: 我需要将集合发送到此方法:

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

and I tried to pass the collection: 我试图通过收藏:

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

but I get a compilation error that the method type is not applicable for List<Client> 但是我收到编译错误,指出方法类型不适用于List<Client>

EDITED: 编辑:

After I added the line: 添加行之后:

clientsToUpdate.toArray(new Client[0]);

I have this compilation error: 我有这个编译错误:

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

The method you mentioned is using varargs, it means it accepts a single Client instance or an array of Client objects. 您提到的方法使用的是varargs,这意味着它接受单个Client实例或Client对象的数组。 You should convert your List to array like this: 您应该像这样将List转换为数组:

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

This should work unless you have multiple Client classes in your project. 除非您的项目中有多个Client类,否则这应该起作用。

T.. is not a collection, it's an array. T ..不是集合,而是一个数组。 So, you have to convert it. 因此,您必须对其进行转换。 Perhaps with something like this: 也许是这样的:

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

EDIT: 编辑:

Oh sorry, I think you want the different way. 哦,对不起,我想您希望使用其他方式。 If you want to pass a Collection to your method, convert it to an array: 如果要将Collection传递给方法,请将其转换为数组:

Object[] array = myCollection.toArray();

You can't pass any Collection to vararg method (unless method signature is (Collection...), but that's almost certainly not what you want here) . 您不能将任何Collection传递给vararg方法(除非方法签名是(Collection ...),但这几乎肯定不是您想要的)。 Try with Array. 尝试使用数组。

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

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