For loop with index kotlin2/15/2024 If you want to iterate through an array or a list with an index, you can do it this way: for (i in array.indices) print (array i) Note that this iteration through a range is compiled down to optimal implementation with no extra objects created. While the first range will hold the list of multipliers corresponding to different rows, on the other hand, the second range will hold the result values. A for loop over an array is compiled to an index-based loop that does not create an iterator object. Further, we’ll need two ranges in our loop. A zero-based index of current iteration is passed as a parameter to action. Kotlin for loop is equivalent to the foreach loop in languages like C. Let’s advance our understanding of looping with multiple variables by applying concepts of ranges and the zip operator to generate multiplication tables, where each row shows up in the format: factor x multiplier = resultĪs the factor will remain the same in all the rows, we’ll have to loop through two multiplier and result variables. (source) Executes the given function action specified number of times. It iterates through arrays, ranges, collections, or anything that provides for iterate.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |