Hi,
How can i fill
prevItems
and
nextItems
of below data class , i have tried
recursion
but it caused infinite loop because we have to find next and previous both but if i try with either previous or next then there is no infinite loop.
data class Root(val item: Int, val prevItems: List<Root>, val nextItems: List<Root>)
Suppose i have data for
item
is => 1,2,3,4,5 then result i wanted is =>
Root(item: 1, prevItems: [], nextItems: [2,3,4,5])
Root(item: 2, prevItems: [1], nextItems: [3,4,5])
Root(item: 3, prevItems: [1,2], nextItems: [4,5])
Root(item: 4, prevItems: [1,2,3], nextItems: [5])
Root(item: 5, prevItems: [1,2,3,4], nextItems: [])
*for making it short i am using 1,2,3… in prevItems/nextItems instead of Root
object
what approach should i use to calculate above? also is there any kotlin collection function can help here ?