6
data class RideDate(var enddate: String? = null,var startdate: String? = null) 



 fun main(args: Array<String>) {
var listOfRideDate = mutableListOf<RideDate>();
val date1 = RideDate()
date1.startdate = "2018-11-05 00:00:00 +0000"
date1.enddate = "2018-11-06 23:59:59 +0000"
listOfRideDate.add(date1)

val date2 = RideDate()
date2.startdate = "2020-01-20 00:00:00 +0000"
date2.enddate = "2020-02-20 00:00:00 +0000"
listOfRideDate.add(date2)

val date3 = RideDate()
date3.startdate = "2020-03-20 00:00:00 +0000"
date3.enddate = "2020-03-20 00:00:00 +0000"
listOfRideDate.add(date3)

val date4 = RideDate()
date4.startdate = "2020-04-20 00:00:00 +0000"
date4.enddate = "2020-04-20 00:00:00 +0000"
listOfRideDate.add(date4)

val date5 = RideDate()
date5.startdate = "2020-11-20 00:00:00 +0000"
date5.enddate = "2020-11-20 00:00:00 +0000"
listOfRideDate.add(date5)


for (i in 0..listOfRideDate.size -1) {
    // we have to remove 2018-11-05 00:00:00 +0000 and 2018-11-06 23:59:59 +0000  from list 
    println(listOfRideDate.get(i).startdate + " and " + listOfRideDate.get(i).enddate)
}

}

This is my data class and the main method I have added item RideDate of startDate and endDate manually I want delete item dynamically if data contains date1.startdate = "2018-11-05 00:00:00 +0000" date1.enddate = "2018-11-06 23:59:59 +0000"

so that I can ignore it please help me how to delete the item from array list in kotlin

3 Answers 3

10

You can use removeAll to remove element from original list if it matches the predicate.

listOfRideDate.removeAll {
    it.startdate == "2018-11-05 00:00:00 +0000" && it.enddate == "2018-11-06 23:59:59 +0000"
}

Or you can filter by creating a new list with filtered items as suggested by Johann Kexel

val filtertedList = listOfRideDate.filter {
    it.startdate == "2018-11-05 00:00:00 +0000" && it.enddate == "2018-11-06 23:59:59 +0000"
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can use filter in your case

val filtertedList = listOfRideDate.filter { element -> someLogik(element)}

Refer also to the following page

Filtering Kotlinlang

4 Comments

can you please help me where I will add this code I am really new in this function what condition and how I will apply here someLogik(element)
how to apply a filter for the start date and end date please help me in this?
@MARSH do you want to remove element from original list or want to create a new filtered one?
someLogik was just an example and symbolizes a function which returns a Boolean and which you can define on yourself. For example fun nonNull(element:RideDate) = element != null . You can use this filtering logic instead or after your foreach loop.
2

Like this:

loop@ for (i in 0 until listOfRideDate.size) {
        // we have to remove 2018-11-05 00:00:00 +0000 and 2018-11-06 23:59:59 +0000  from list
        if (listOfRideDate.get(i).startdate == "2018-11-05 00:00:00 +0000" &&  listOfRideDate.get(i).enddate == "2018-11-06 23:59:59 +0000"){
            listOfRideDate.removeAt(i)
            break@loop
        }
    }

or

listOfRideDate.removeIf {
     it.startdate == "2018-11-05 00:00:00 +0000" &&  it.enddate == "2018-11-06 23:59:59 +0000"
}

7 Comments

its show Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
you cannot remove something within a loop. If you want to change your current list, you have to first save your element(s) within the loop and then use remove(element) or removeAll(elements) on the collection (Or of course also removeAt(index)) outside the loop
No, you can, Just need to stop for loop.
Right, you can remove one item
Sure you can do this, if you only want to remove one element.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.