vendredi 24 juin 2016

Function that receive collection of objects that implement some interface [duplicate]

I implemented my own JsonObject and JsonArray classes in Java, and created an interface JsonObjectConvertable that my other classes implement, so I can convert them all to the JsonObject. I made the next static function in my JsonArray class:

public static JsonArray toJsonArray(Collection<JsonObjectConvertable> collection){
    JsonArray result = new JsonArray();
    for (JsonObjectConvertable item : collection)
        result.add(item.ToJsonObject());
    return result;
}

I have a class, say myObject that implements JsonObjectConvertable, but when I try to use the ToJsonArray method, it says it cannot be applied to the function.

The code that has the error

List<myObject> list = new ArrayList<myObject>();
//some code that fills the list
JsonArray jsonArray = JsonArray.ToJsonArray(list); // <- error!!!

JsonObjectConvertable.java :

public interface JsonObjectConvertable {
    JsonObject ToJsonObject();
}

I also tried with Iterable<JsonObjectConvertable> instead the Collection<JsonObjectConvertable> in the ToJsonArray, same error...

Thanks :)

Aucun commentaire:

Enregistrer un commentaire