6. Working With Collections¶
6.1. Working With Lists¶
-
In a Kotlin or Kotlinscript file, write some code that creates a small list contains between 5 and 10 integer values.
-
Add code that will
-
Print out the contents of the list (without using a loop)
-
Print a count of how many elements are in the list (without using a loop)
-
Display the first three and last three values (e.g., using the
take
andtakeLast
methods) -
Print a reversed version of the list, leaving the original unchanged
-
Print a sorted version of the list, leaving the original unchanged
-
Filter the list, displaying only the odd numbers
-
Here’s an example of what program output should look like:
List is [1, 2, 7, 4, 11, -1, 8] List has 7 values First three values are [1, 2, 7] Last three values are [11, -1, 8] Reversed list is [8, -1, 11, 4, 7, 2, 1] Sorted list is [-1, 1, 2, 4, 7, 8, 11] Odd values are [1, 7, 11, -1]
6.2. Reading From a File Into a List¶
-
In a new directory (or a new IntelliJ project), create a Kotlin file named
stats.kt
. In this file, add the following code:import java.io.File import java.util.Scanner fun main(args: Array<String>) { if (args.size > 0) { val input = Scanner(File(args[0]) } }
When it runs, this program expects a filename to be supplied as a command line argument. It uses this filename to construct a
File
object representing the named file, then creates aScanner
object attached to this file. TheScanner
object can be used to parse the contents of the file.Note
The important point here is that
File
andScanner
are part of Java’s API. Java classes can be used seamlessly within Kotlin code.Check that the program compiles and runs before proceeding further.
-
Add the following code to
stats.kt
:val data = mutableListOf<Double>() while (input.hasNextDouble()) { data.add(input.nextDouble()) }
This creates an empty list, then uses the
Scanner
to readDouble
values from the file and add them to this list. -
Add code that displays the number of values that have been added to the list.
-
Add code that computes and displays the mean and standard deviation of values in the list. Note that computing the mean can be done with a single method call!
Program output should look something like this:
6 values read from file Mean value = 3.5917 Std deviation = 1.1082
6.3. Working With Maps¶
-
In a Kotlin or Kotlinscript file, write some code that creates a small map of
String
toInt
, containing a few key-value pairs. Your program should-
Print out the contents of the map (without using a loop)
-
Print a count of how many key-value pairs it contains (without using a loop)
-
Use a for loop to sum up the values, then print the sum
-
Remove one of the key-value pairs, then print map contents again
Here’s an example of what program output should look like:
Map is {oranges=1, bananas=7, plums=5, apples=2} 4 key-value pairs, sum of values = 15 Removing oranges... Map is now {bananas=7, plums=5, apples=2}
-
-
Create a new directory (or new IntelliJ project) and put
input.kt
in it. Add a new file,phonebook.kt
. In this new file, write a program that creates and maintains a phonebook, using a map defined like so:val phonebook = mutableMapOf<String,String>()
Your program should use a loop to repeatedly prompt the user to enter a name. If the string is empty (i.e., the user has simply pressed the Enter key) then the program should break out of the loop; otherwise, it should use the entered name to look up the corresponding phone number in
phonebook
.If a number is found for the entered name, it should be displayed; otherwise, the program should print “Not found”, the user should be prompted to supply a phone number and then the name and number should be inserted into the map.
Here’s an example of what interaction with the program should look like:
Enter a name: nick Not found Enter number for nick: 0123 456789 Enter a name: nick Number for nick is 0123 456789