There is error in following code when we run in Kotlin coderpad -
Any help and how to fix it ?
import java.io.*;
import java.util.*;
// Fun with observers
// A generic observer
interface Observer {
val id: Int
get() = 0
fun notify(event: Int)
}
// A list of observers, supports:
// - adding observers
// - removing observers
// - notifying all observers
class ObserverList {
private var observers = ArrayList<Observer>()
fun addObserver(observer: Observer) {
observers.add(observer)
}
fun removeObserver(observer: Observer) {
for (i in 0 until observers.count()) {
if (observer.id == observers[i].id) {
observers.removeAt(i)
break
}
}
}
fun notifyAll(event: Int) {
for (i in 0 until observers.count()) {
observers[i].notify(event)
}
}
}
//---
var observerList = ObserverList()
val STARTUP_EVENT: Int = 1
val INTERESTING_EVENT: Int = 2
val SHUTDOWN_EVENT: Int = 3
class MyObserver: Observer {
companion object {
var counter: Int = 1
}
private val identifier: Int
constructor() {
identifier = counter++
}
override val id: Int
get() = identifier
override fun notify(event: Int) {
when (event) {
STARTUP_EVENT -> {
println("$identifier: Got startup event!")
}
INTERESTING_EVENT -> {
println("$identifier: Got interesting event!")
}
SHUTDOWN_EVENT -> {
println("$identifier: Got shutdown event!")
// clean up time!
observerList.removeObserver(this)
}
else -> {
print("Unknown event!")
}
}
}
}
//---
fun main() {
println("Welcome to the wonderful world of observer lists!")
val numObservers: Int = 9
for (i in 0 until numObservers) {
observerList.addObserver(MyObserver())
}
observerList.notifyAll(STARTUP_EVENT)
// Do a bunch of interesting work and let the observers know about it!
observerList.notifyAll(INTERESTING_EVENT)
// Time to shutdown
observerList.notifyAll(SHUTDOWN_EVENT)
println("Done")
}Error :
ran 102 lines of Kotlin (finished in 15.56s):
Welcome to the wonderful world of observer lists!
1: Got startup event!
2: Got startup event!
3: Got startup event!
4: Got startup event!
5: Got startup event!
6: Got startup event!
7: Got startup event!
8: Got startup event!
9: Got startup event!
1: Got interesting event!
2: Got interesting event!
3: Got interesting event!
4: Got interesting event!
5: Got interesting event!
6: Got interesting event!
7: Got interesting event!
8: Got interesting event!
9: Got interesting event!
1: Got shutdown event!
3: Got shutdown event!
5: Got shutdown event!
7: Got shutdown event!
9: Got shutdown event!
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 4
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) (finished in 8.54s):
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at ObserverList.notifyAll(solution.kt:36)
at SolutionKt.main(solution.kt:99)
at SolutionKt.main(solution.kt)