Return to Index
Equality and Object Copying : 07/06/2022
Polymorphism : 07/06/2022
Inheritance : 07/05/2022
Data Modeling 1 : 07/05/2022
Companion Objects : 07/01/2022
Encapsulation : 07/01/2022
Constructors : 06/30/2022
Objects, Continued : 06/30/2022
Introduction to Objects : 06/29/2022
Compilation and Immutability : 06/29/2022
Practice with Collections : 06/28/2022
Maps and Sets : 06/28/2022
Lists and Type Parameters : 06/27/2022
Imports and Libraries : 06/27/2022
Multidimensional Arrays : 06/24/2022
Practice with Strings : 06/24/2022
null : 06/23/2022
Algorithms and Strings : 06/23/2022
Strings : 06/22/2022
Functions and Algorithms : 06/22/2022
Practice with Functions : 06/21/2022
More About Functions : 06/21/2022
Errors and Debugging : 06/20/2022
Functions : 06/20/2022
Practice with Loops and Algorithms : 06/16/2022
Algorithms I : 06/16/2022
Loops : 06/15/2022
Arrays : 06/15/2022
Compound Conditionals : 06/14/2022
Conditional Expressions and Statements : 06/14/2022
Operations on Variables : 06/13/2022
Variables and Types : 06/13/2022
Welcome to CS 124 : 06/13/2022
Practice with Functions
Kotlin
Created By: Geoffrey Challen
/ Updated: 2022-06-14
This lesson continues our exploration of functions.
We'll present a bit more Kotlin syntax, and spend time reinforcing what we've learned about functions.
Let's get started!
for in
Loop
To get us warmed up and ready to go, let's check out a new bit of Kotlin syntax!
Remember how we started with this common while
loop:
and eventually arrived at this common for
loop:
Well, that for
loop became so common that there's an even simpler way to work through the values in array using Kotlin's for in
loop:
Show off the enhanced for loop or foreach
loop.
Interactive Walkthrough
Click on an icon below to start!
Indexed v. Non-Indexed for
Loop
When you should use the indexed for
(for (i in 0 until value
) and when for in
?
(Technically they are both for in
loops, but the difference is whether the variable hold the index or a value from the array.)
Here are some things to consider:
- If you are iterating over an array—or other data structures that support iteration, which we'll encounter soon—consider the
for in
loop - However, if you need access to the array index, then you have no option but to use the indexed
for
loop
For many common array-processing tasks that we've encountered, the for in
loop is a much better fit, since avoiding the extra index variable leads to a cleaner loop declaration and value access within the loop.
For example, counting:
Show how to complete the homework problem above. Feel free to cover multiple approaches!
Practice with Functions
Next let's get some more practice with functions!
Together we'll write a method that determines when an IntArray
is a palindrome array: meaning that the values it contains are the same forward and backward.
For example, {1, 2, 4}
is not an array palindrome, but {1, 0, 2, 0, 1}
is!
Let's go step by step and see how to approach constructing this method.
First, let's determine our method signature, and practice calling it on some sample inputs.
Show how to set up the method signature, and call it using a few sample inputs.
Interactive Walkthrough
Click on an icon below to start!
Next, let's begin work on the body of the method.
Our array access pattern here is a bit different than what we've seen previously, so let's proceed carefully.
Write the loop that proceeds through the array forward, but also show how we can construct a backward index as we go.
Don't worry about only processing half of the array.
Interactive Walkthrough
Click on an icon below to start!
As a next step, let's complete the job by adding the decision-making logic we need to determine if the passed array is an array palindrome.
This should remind us a bit of the search pattern that we covered yesterday.
Add the decision making logic in the loop, and discuss short-circuit return or proof by contradiction.
Interactive Walkthrough
Click on an icon below to start!
Finally, let's make one small improvement to our code.
Discuss the optimization achieved by only processing the first half of the array.
Interactive Walkthrough
Click on an icon below to start!
Show how to complete the homework problem above. Feel free to cover multiple approaches!