我尝试通过将Java实现转换为kotlin来学习Kotlin。 目前,我停留在Kotlin的以下错误消息“未解决的参考:removeAt”
这是我的Kotlin代码:
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
}
}
}
这里是原始的Java实现:
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;
}
}
}
如何删除特定任务? 我必须将声明更改为:
private val mActivityTaskMap = mutableMapOf<String, MutableList<CustomAsyncTask<*, *, *>>>()
答案 0 :(得分:2)
您要编辑的列表在地图内是不变的。使其可变:
private Map<String, MutableList<CustomAsyncTask<?,?,?>>> mActivityTaskMap;