0

I'm trying to convert mutablelist to jsonarray to be able to send it to next activity since it seems to be impossibe to just pass the damn mutablelist without adding any serialization plugin

This is what I have so far:

val arrr: Array<JSONObject> = arrayOf()
                var curr = 0
                for(jsonIndex in 0 until memes.size - 1) {
                    val rootObject = JSONObject()
                    rootObject.put("nickname", memes[jsonIndex].nickname)
                    rootObject.put("title",memes[jsonIndex].title)

                    arrr[curr] = rootObject
                    curr++
                }

I get the error:

ArrayIndexOutOfBoundsException: length=0; index=0

referred to the line

arrr[curr] = rootObject

Whats's wrong here?

2
  • Your arrr array is an array of length zero because you instantiated it with nothing to put in it. Commented Mar 5, 2020 at 18:06
  • Comming from PHP where working with arrays is plain simple I don't understand whate exactly you mean :/ What do you mean by "you instantiated it with nothing to put in it"? In PHP it's $arr = array(); $arr[] = putwhateveryouliketheredontneedtodoadditionalcrap Commented Mar 5, 2020 at 18:09

1 Answer 1

1

When you instantiate an Array, it has a fixed size, forever (although if it's a var you could assign a new, bigger array to the variable). If you want something that can grow as needed, use a MutableList. But even with a List, you can't jump ahead to an index that hasn't been created yet. You use the add function instead:

val arrr: MutableList<JSONObject> = mutableListOf()

//...

for(jsonIndex in 0 until memes.size - 1) {
    val rootObject = JSONObject()
    rootObject.put("nickname", memes[jsonIndex].nickname)
    rootObject.put("title",memes[jsonIndex].title)

    arrr.add(rootObject)
}
Sign up to request clarification or add additional context in comments.

9 Comments

These all have very different meanings that become clear as you use it more. These languages are lower level than PHP and Javascript. When you have more control over the way underlying data is stored, you have a lot more power to do specific things, optimize speed and memory use, etc, at the expense of more tedious detail you have to pay attention to in your code. For example, it wouldn't be possible to program a game with advanced graphics in Javascript without having lots of stuttering from all the memory manipulation you as the programmer can't control.
On the other hand, a tool like FFMPEG, which is used for video manipulation has to be written in an even lower level language (Assembly) to squeeze every ounce of performance possible because it is handling such complex tasks. But that language takes a lot more attention to detail than Java/Kotlin/C++, etc.
Oh I didn't think about performance, yeah that makes sense, thanks!
And if you compare Kotlin to Java, there are areas where a newcomer might think Kotlin looks a lot more tedious because you have to pay attention to whether variables are nullable or whether lists are mutable. But these features are welcomed by those who've done very much bugfixing of Java code. They force the programmer to write less bug-prone code from the beginning.
Yeah that's something I noticed fast, no null safety is nice. I broke up learning JAVA mainly because of the boilerplate code and switched to kotlin, didn't regret that
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.