Home > Mobile >  find value in arraylist in kotlin
find value in arraylist in kotlin

Time:02-19

Hey I am working in kotlin. I am working on tree data structure. I added the value in list and now I want to find that value and modified their property. But I am getting the error.

enter image description here

I want to find that node value and modified other property.

CodePudding user response:

Your class VariantNode only has a single no-arg constructor, but you're trying to call it with arguments, hence the error

Too many arguments for public constructor VariantNode() defined in com.example.optionsview.VariantNode

Either you have to provide a constructor, that matches your call, e.g.

open class VariantNode(var value: ProductValue?) {
    var children: MutableList<VariantNode> = arrayListOf()
}

or you need to adjust your code to use the no-arg constructor instead.

val node = VariantNode()
node.value = strength.value

baseNode.children.contains(node)

Note however, that your call to contains most likely will not work, because you do not provide a custom implementation for equals. This is provided by default, when using a data class.

If you just want to validate whether baseNode.children has any element, where value has the expected value, you can use any instead, e.g.:

baseNode.children.any { it.value == strength.value }
  • Related