Kotlin’s handling of arrays
fun main() {
val names = arrayOf("John", "Jane", "Jill", "Joe") // Assumes Strings
val longs1 = arrayOf(1L, 2L, 3L) // State Long by use of L
val longs2 = arrayOf<Long>(1, 2, 3, 4) // Or by declaring datatype in arrayOf<>
val longs3 = arrayOf(1, 2, 3, 4) // Otherwise will assume Ints
println(longs2 is Array<Long>)
// println(longs3 is Array<Long>) // Will fail because is Int
println(longs3 is Array<Int>)
println(longs1[2])
val evenNumbers = Array(16) {i -> i*2} // Use lambda expression to get even numbers up to 30
for (number in evenNumbers) {
println(number)
}
val lotsOfNumbers = Array(100001) {i -> i} // Numbers from 0 to 100,000
val allZeroes = Array(100) {0} // One hundred zeroes
var someArray: Array<Int> // If you don't want to initialise it till later
someArray = arrayOf(1, 2, 3, 4)
for (number in someArray) {
println(number)
}
someArray = Array(6) {i -> (i + 1) * 10}
for (number in someArray) {
println(number)
}
val mixedArray = arrayOf("hello", 22, BigDecimal(10.5), 'a') // Can have array of mixed datatypes i.e. Any
for (element in mixedArray) {
println(element)
}
val myIntArray = arrayOf(3, 99, 234, 54)
// DummyClass().printNumbers(myIntArray) // Fails because it expects IntArray but found Array<Int>
val myIntArray2 = intArrayOf(3, 99, 45, 756) // Use specific Array types
DummyClass().printNumbers(myIntArray2) // Works!
// var someOtherArray = Array<Int>(5) // Fails. By declaring the size (5) we are actually trying to instantiate the array, so we would need to add values
var someOtherArray: Array<Int> // This is okay
var anotherArray = IntArray(5) // However, primitive type arrays allow this as they're initialised to default, in this case all zeroes
DummyClass().printNumbers(anotherArray)
// So can you instantiate an array of fixed size? An array of nulls. Potentially risky as could lead to NullPointerExceptions, so you have to explicitly ask for them.
// Not a problem with primitive arrays as they are initialised with defaults e.g. zeroes for IntArray
// However, array of Objects is potentially null, so we use ArrayOfNulls to warn compiler
val nullableInts = arrayOfNulls<Int>(5)
for (i in nullableInts) {
println(i)
}
// Try nullableInts[0]. and see how few options are available
nullableInts[3] = 5
val intValue = nullableInts[3]?.rem(2) // Now options are available provided you use ?
// If you want pass an Array<Int> to Java you can convert it:
DummyClass().printNumbers(evenNumbers.toIntArray()) // evenNumbers was an Array<Int>
// And vice versa
val convertedIntArray = myIntArray2.toTypedArray() // Converts a primitive IntArray to Array<Int>
}
public class DummyClass {
public String isVacationTime (boolean onVacation) {
return onVacation ? "I'm on vacation" : "I'm working";
}
public void printNumbers (int[] numbers) {
for (int number: numbers) {
System.out.println(number);
}
}
}
