I have a class which I want to use, to filter my array according to driver. Driver is hardcoded just to try and eliminate possibilities of why the code is not working. I have tried a lot of different methods none seem to working. I get the error IndexOutOfBoundsException: Index: 1, Size: 0 after my line of println(driverList[3].DELNO).
private fun datafilter(tripsheetlist: ArrayList<DataModel>, driver: String): List<DataModel> {
println("driver in datafilter subclass")
println(driver)
println("tripsheetlist[3].DELNO")
println(tripsheetlist[3].DELNO)
var driverList: List<DataModel> = tripsheetlist
// var driverList : ArrayList<DataModel> = tripsheetlist.filter { s -> s == driver }
var i = 0
while ( i != tripsheetlist.size){
driverList = tripsheetlist.takeWhile { tripsheetlist[i].DRIVER == "JIM" }
i++
}
println("driverList[3].DELNO")
println(driverList[3].DELNO)
return driverList
}
Below is DataModel
class DataModel(
var TRIPSHEETNO: Int,
var WONUMBER: String,
var DELNO: Int,
var CUSTOMER: String,
var DRIVER: String,
var WEIGHT: Double,
var state: DataState = DataState.Unselected
)
After the filter I have seen it returns as an List and not ArrayList. I have established that this does not affect my data.
Thank you for all suggestions.
Solution :
private fun datafilter(tripsheetlist: ArrayList<DataModel>, driver: String): ArrayList<DataModel> {
return ArrayList(tripsheetlist.filter { it.DRIVER == driver }) }
ArrayListunless you have a good reason - in Kotlin you generally just use theListandMutableListtypes, so you don't worry about the exact implementation, and you don't need to go converting things toArrayListorHashMapor whatever just to make them work with your other functions (this goes for Java too!)