每個人。我是科特林的新手。有沒有人有解決這個語法錯誤的方法。關鍵字“我的代碼中的索引不斷標記一個錯誤,指出未決議的參考:索引。我嘗試使用 ID 'kotlin-android-extensions' 更新我的 build.gradle,但似乎沒有解決問題。
fun detachCounter(row: Int , col: Int) {
var index = -1
for (i in catalog.indices) {
if (catalog[i].col == col && catalog[i].row == row) {
index = i
break
}
}
if (index != -1) {
catalog.removeAt(index)
_draughBoard[row][col] = 0
}
}
uj5u.com熱心網友回復:
假設目錄是類DraughtCounters的 ArrayList,您的代碼應該可以作業。我用一個非常簡單的DraughtCounters類對其進行了測驗:
class DraughtCounters(var row: Int = 0, var col: Int = 0)
val catalog = arrayListOf(
DraughtCounters(1, 1),
DraughtCounters(1, 2),
DraughtCounters(1, 3),
DraughtCounters(2, 1),
DraughtCounters(2, 2),
DraughtCounters(2, 3),
DraughtCounters(3, 1),
DraughtCounters(3, 2),
DraughtCounters(3, 3)
)
... your detachCounter function code here ...
detachCounter(1, 2)
detachCounter(2, 3)
detachCounter(3, 1)
for (item in catalog) {
println("${item.row}, ${item.col}")
}
如果這確實是您對目錄和DraughtCounters 的設定,那么您可以進一步將detachCounter簡化為如下所示:
fun detachCounter(row: Int, col: Int) {
val itemToDetach = catalog.firstOrNull { it.row == row && it.col == col }
if (itemToDetach != null) {
catalog.remove(itemToDetach)
// _draughBoard[itemToDetach.row][itemToDetach.col] = 0
}
}
uj5u.com熱心網友回復:
我通過簡單地更新我的構建解決了錯誤。gradle 依賴項
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361486.html
標籤:科特林
