我想编写一个函数来返回List不是第一个或最后一个项目(通过点)的每个项目.该函数获取通用List<*>作为输入.只有在列表的元素属于以下类型时才应返回结果Waypoint:
fun getViaPoints(list: List<*>): List<Waypoint>? {
list.forEach { if(it !is Waypoint ) return null }
val waypointList = list as? List<Waypoint> ?: return null
return waypointList.filter{ waypointList.indexOf(it) != 0 && waypointList.indexOf(it) != waypointList.lastIndex}
}
Run Code Online (Sandbox Code Playgroud)
当转换为List<*>to时List<Waypoint>,我收到警告:
未选中Cast:kotlin.collections.List to kotlin.colletions.List
我不知道如何实现它.在没有此警告的情况下实施此功能的正确方法是什么?
拥有以下代码:
fun doSomething(): List<String> {
val test: List<*> = arrayListOf("test1", "test2")
return test as List<String>
}
Run Code Online (Sandbox Code Playgroud)
有没有办法抑制最后一行出现的未经检查的投射警告?我试图@SuppressWarnings("unchecked")在方法级别使用标准的Java 方法,但它没有用.