简体   繁体   中英

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>

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. You should convert your List to array like this:

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.

T.. is not a collection, it's an array. 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:

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) . Try with Array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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