Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Unresolved reference: removeAt

Tags:

kotlin

I try to learn Kotlin by converting my java implementation to kotlin. Currently I stuck at the following error message from Kotlin "unresolved reference: removeAt"

Here is my Kotlin code:

private val mActivityTaskMap = mutableMapOf<String, List<CustomAsyncTask<*, *, *>>>()

fun removeTask(task: CustomAsyncTask<*, *, *>) {
    for ((key, tasks) in mActivityTaskMap) {
        for (i in tasks.indices) {
            if (tasks[i] === task) {
                tasks.removeAt(i)  // <==== ERROR
                break
            }
        }
        if (tasks.size == 0) {
            mActivityTaskMap.remove(key)
            return
        }
    }
}

And here the original java implementation:

private Map<String, List<CustomAsyncTask<?,?,?>>> mActivityTaskMap;

public void removeTask(CustomAsyncTask<?,?,?> task) {
    for (Map.Entry<String, List<CustomAsyncTask<?,?,?>>> entry : mActivityTaskMap.entrySet()) {
        List<CustomAsyncTask<?,?,?>> tasks = entry.getValue();
        for (int i = 0; i < tasks.size(); i++) {
            if (tasks.get(i) == task) {
                tasks.remove(i);
                break;
            }
        }

        if (tasks.size() == 0) {
            mActivityTaskMap.remove(entry.getKey());
            return;
        }
    }
}

How can I remove the specific task? Must I change the declaration to:

private val mActivityTaskMap = mutableMapOf<String, MutableList<CustomAsyncTask<*, *, *>>>()
like image 741
Frank IstDochEgal Avatar asked Mar 19 '26 07:03

Frank IstDochEgal


1 Answers

The list you're trying to edit is immutable inside the map. Make it mutable:

private Map<String, MutableList<CustomAsyncTask<?,?,?>>> mActivityTaskMap;
like image 164
Neo Avatar answered Mar 22 '26 12:03

Neo