Kevin Janvier Chinabalire
01/31/2018, 10:08 AMvar model:User()
forEachIndexed { index, users ->{
println("my $users ")
}
println("I want to access my result outside for loop")
edwardwongtl
01/31/2018, 10:20 AMKevin Janvier Chinabalire
01/31/2018, 10:22 AMprintln("Result $model")
... Not an arraylistedwardwongtl
01/31/2018, 10:24 AMKevin Janvier Chinabalire
01/31/2018, 10:24 AMval arraylist = ArrayList<User>()
Kevin Janvier Chinabalire
01/31/2018, 10:25 AMval arraylist = ArrayList<User>()
var model:User()
forEachIndexed { index, users ->{
println("my $users ")
}
println("Result $model")
edwardwongtl
01/31/2018, 10:27 AMarraylist.forEachIndexed
?Kevin Janvier Chinabalire
01/31/2018, 10:28 AMedwardwongtl
01/31/2018, 10:28 AMArrayList<User>
, then you can use filter{}
to get the matches, or even first {}
if you just need one resultKevin Janvier Chinabalire
01/31/2018, 10:28 AMarraylist.forEachIndexed
Kevin Janvier Chinabalire
01/31/2018, 10:30 AMval arraylist = ArrayList<User>()
var model:User()
arraylist.forEachIndexed { index, users ->{
println("my $users ")
}
println("Result $model")
where i need help is to access that model ... which is not an arraylist model just a model like aboveedwardwongtl
01/31/2018, 10:31 AMvar model
to the first one that matches some condition inside the val arraylist
?Kevin Janvier Chinabalire
01/31/2018, 10:33 AMvar model
which i declare up ...Kevin Janvier Chinabalire
01/31/2018, 10:34 AMmodel
it cannot be an array
edwardwongtl
01/31/2018, 10:35 AMfun
inside the loop?Kevin Janvier Chinabalire
01/31/2018, 10:37 AMedwardwongtl
01/31/2018, 10:39 AMfun
for every User
in the list, then use filter
to get rid of the unwanted User
first, then map
each User
to the result of that fun
Kevin Janvier Chinabalire
01/31/2018, 10:56 AMfun
inside a for
cz filtering is not helpingKevin Janvier Chinabalire
01/31/2018, 10:57 AMvar getuser:List<User> = arralist.filter { user -> User ==User() }
println("FILTER 909 $getuser")
getuser.map { dataModel ->
log("DADADADADDAAD $dataModel")
}
edwardwongtl
01/31/2018, 11:00 AMfun action(user: User): T = ...
val arraylist: ArrayList<User>
arraylist.filter { user -> user == ... }.map { action(it) }
If action()
does not return a value, replace the .map
with .forEach
Kevin Janvier Chinabalire
01/31/2018, 11:05 AMarraylist.filter { user -> user == User() }.map
edwardwongtl
01/31/2018, 11:06 AM.map { User() }
Kevin Janvier Chinabalire
01/31/2018, 11:09 AMedwardwongtl
01/31/2018, 11:10 AMuser == User()
edwardwongtl
01/31/2018, 11:10 AMfilter
is not meant for manipulating dataKevin Janvier Chinabalire
01/31/2018, 11:15 AMedwardwongtl
01/31/2018, 11:19 AMedwardwongtl
01/31/2018, 11:22 AMArrayList<User>
, and you want to pass some of them into a function fun action(user: User): Result
, and gather all the results.
Am I getting it wrong, or you want to actually manipulate the ArrayList<User>
?Kevin Janvier Chinabalire
01/31/2018, 12:04 PMKevin Janvier Chinabalire
01/31/2018, 12:05 PMArrayList<User>
i want to access the model by loopingedwardwongtl
01/31/2018, 12:13 PMusers.filter { boolean condition }.map { user -> getResult(user) }
This will gives you back List<Result>
which you can then operate on