Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Kotlin: save null value in ArrayList?

I have created an ArrayList of nullable types, but when I try to add a null value to the list the null value doesn’t get added.
eg.

var intArray: ArrayList<Int?>? = null
intArray = ArrayList()

intArray.add(0)
intArray.add(null)
intArray.add(2)

This code returns an array with only 2 elements, ignoring the null value.
What I want to achieve is intArray.get(1) where it will return a null value.

Is there a way I can do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

ArrayList is so old school, better you using – if possible – list:

   val xs = listOf<Int?>(1,2,null,4,5)

   println(xs)          // [1, 2, null, 4, 5]

   println(xs.get(2))   // null 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading