can anyone tell me why this error iss happening ``...
# announcements
o
can anyone tell me why this error iss happening
Copy code
java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
In this code
Copy code
fun getMarketMap(stats: MarketStats): Map<String,MarketResponse> {
        var map = mutableMapOf<String,MarketResponse>()
        for(i in 0 until (stats.marketPrice.size)) {
            println("$i th: ${locations[i]}")
            map[locations[i]] = stats.marketPrice[i]
        }
        return map
    }
and if possible any way to improve the code
d
could be your locations... how many items does it have? for 6 items, the maximum index is 5...
j
I'd suggest taking a look at IntRange
k
Impossible to say without more context, but I am going to guess locations is too small ­čÖé
ÔśŁ´ŞĆ 1
Perhaps functionalise it and take each invocation on the range, one step at a time
o
Copy code
val locations = listOf("Caerleon","Lymhurst","Martlock","Bridgewatch",
        "FortSterling","Thetford")
thats the locations list
d
whats your stats.marketPrice.size ?
o
idk, let me deebug it
I see now whats the problem. The size of stats.marketprize,size is 30 Thnx for your help guyss
­čĹŹ 2
a
Copy code
stats.marketPrice
    .mapIndexed { index, element ->
        println(whatever you need)
        locations[index] to element
    }
    .toMap()
you could also use the
zip
function
generally, prefer the countless extensions on
Iterable
that Kotlin provides over iterating in loops