Good day. So I came from java and now doing kotlin. What I want is very simple but I cant seem to get it right.
var j: Int = 4
for (i in 0 until movieList.size) {
sum = movieList.get(i)(j)
//Answer from @lukas.j - It was as simple as sum = movieList[i].Weight
}
I first tried it without the .get, then I saw it didn't understand what I was talking about so google told me to use .get.
This is probably a simple fix if someone can just show me how please. The array is a 2d array list.
I tried a suggestion and this error shows.
Another example of a different attempt with a different error.
Adding my array:
val movieList = ArrayList<MovieModel>().apply {
add(MovieModel(190617, 182832, "Jcorp", 100,"Delivery done", "Delivery not done",""))
add(MovieModel(190617, 182833, "Honda", 100,"No exceptions", "Exceptions",""))
add(MovieModel(190617, 182832, "Everflu", 100,"100%", "50%",""))
add(MovieModel(190617, 182832, "Panado", 100,"OK", "NO",""))
add(MovieModel(190617, 182832, "Gheiters", 100,"Success", "Failed",""))
add(MovieModel(190617, 182832, "John", 100,"Yes", "No",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(9190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(190617, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(10, 182832, "Everflu", 100,"false", "false",""))
add(MovieModel(10, 182832, "2010", 100,"false", "false",""))
add(MovieModel(10, 182832, "2010", 100,"false", "false",""))
add(MovieModel(10, 182832, "2010", 100,"false", "false",""))
add(MovieModel(10, 182832, "2010", 100,"false", "false",""))
add(MovieModel(10, 182832, "2010", 100,"false", "false",""))
add(MovieModel(10, 182832, "2010", 100,"false", "false",""))
add(MovieModel(10, 182832, "2010", 100,"false", "false",""))
}
Movie model:
package com.dispatch.tripsheet
class MovieModel(
var WOrder: Int,
var DElNote: Int,
var Company: String,
var Weight: Int,
var Button1: String,
var Button2: String,
var tvdone: String
)
CodePudding user response:
I would suggest to use a data class instead of a class:
data class MovieModel(
var WOrder: Int,
var DElNote: Int,
var Company: String,
var Weight: Int,
var Button1: String,
var Button2: String,
var tvdone: String
)
Creating the list can be done with listOf:
val movieList = listOf(
MovieModel(190617, 182832, "Jcorp", 100, "Delivery done", "Delivery not done", ""),
MovieModel(190617, 182833, "Honda", 100, "No exceptions", "Exceptions", ""),
MovieModel(190617, 182832, "Jcorp", 100, "Delivery done", "Delivery not done", "")
)
And you can get and set the weight like this:
val movie = movieList[0]
println(movie.Weight) // Output: 100
movie.Weight = 125
println(movie.Weight) // Output: 125
CodePudding user response:
The syntax you're looking for is (see Indexed access operator):
movieList[i][j]
Alternatively, you may use the get
syntax:
movieList.get(i).get(j)
However this feels unnecessarily verbose.