Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find the position of lastIndexOf a List that start with a certain character in kotlin/Java

var list_a = listOf("00:00", "09:00", "20:00", "23:00", "01:00", "03:00")

// i want to return the position of "23:00" in list_a

var list_b = listOf("00:10", "00:30", "09:00", "21:10")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// i want to return the position of "21:10" in list_b

how do i write a function to get the position of starting with 2X:XX?
How can I mix lastIndexOf() and startsWith()

>Solution :

You can use normal for-loops, i.e.

int getIndexOfLastStringStartingWith2(List<String> list) {
   for (int i = list.size() - 1; i > 0; i--) {
      if (list.get(i).startsWith("2")) {
        return i;
      }
   }

  return -1; 
}

This will return the index of the last string in the list starting with 2.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading