최근 Kotlin을 배우기 시작 했으므로 일부 데이터 구조를 구현하기로 결정했습니다. 그래서, 나는 단일 연결리스트 구현 시도했다 :Kotlin의 링크 된 목록 구현
package datastructures
public class LinkedList {
private data class Node(var nodeValue: Int, var next: Node? = null)
private var head: Node? = null
fun insert(n: Int) {
if(head == null) head = Node(n)
else {
var cur = head
while(cur?.next != null) {
cur = cur?.next
}
cur?.next = Node(n)
}
}
fun print() {
var cur = head
while(cur != null) {
print("${cur.nodeValue} ")
cur = cur?.next
}
}
}
fun main(args: Array<String>) {
val n = LinkedList()
n.insert(5)
n.insert(3)
n.print()
}
을 나는 다음과 같은 오류 있어요 :
Error:(22, 13) Kotlin: [Internal Error] org.jetbrains.jet.codegen.CompilationException: Back-end (JVM) Internal error: cannot store to value [email protected]
Cause: cannot store to value [email protected]
File being compiled and position: (22,13) in C:/Users/Khaled/IdeaProjects/Kotlin/src/LinkedList.kt
PsiElement: cur?.next = Node(n)
The root cause was thrown at: StackValue.java:75
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:243)
at org.jetbrains.jet.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:262)
at ...
내가 여기 구글에서 검색 있었어요을하지만 난 알아낼 수 없습니다 이 오류를 일으키는 문제는 무엇
편집 : 그래서 나는하려 한 insert
기능을 다시 구현하고 완 피하기 위해 requireNotNull()
를 사용 안전하지 않은 것에 대해 걱정하지 마십시오. 여기
fun insert(n: Int) {
if (head == null) head = Node(n)
else {
var cur = head!!
while (cur.next != null) {
cur = cur.next!!
}
cur.next = Node(n)
}
}
당신이 코 틀린의 어떤 버전을 사용합니까 : 문제는 그 값이
null
을 점검하기 전에 지역 변수에로드해야하므로 컴파일러는, 필드는 언제든지 변경할 수 있습니다var
로 선언 있다고 가정입니까? – abacabadabacaba@abacabadabacaba 0.8.11 –
참고 : 내부 컴파일러 오류는 Kotlin의 오래된 베타 버전이었습니다. –