initialize

How to create and populate Kotlin lists (initialize mutable and immutable lists)

If you ever need to create and populate a Kotlin list, I can confirm that these approaches work for an immutable and mutable lists:

// fill an immutable list
val doubles = List(5) { i -> i * 2 }

// fill a mutable list of ten elements with zeros
val ints = MutableList(10) { 0 }

The Kotlin REPL shows how these approaches work:

Scala: How to fill/populate a list (same element or different elements)

As a quick note, if you ever need to fill/populate a Scala list with the same element X number of times, one solution is to use the fill method that’s available to Scala sequences, like this:

scala> val x = List.fill(3)("foo")
x: List[String] = List(foo, foo, foo)

If you want to populate a list with different element values, another approach is to use the tabulate method:

How to populate a Java int array with a range of values

Here’s an easy way to populate/initialize a Java int array with data, such as a range of numbers. The key is to use the rangeClosed method on the Java 8 IntStream class. Here’s an example using the Scala REPL:

scala> val n = java.util.stream.IntStream.rangeClosed(0, 10).toArray()
n: Array[Int] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

I show that in Scala to show the output, and here’s what it looks like with Java:

How to create a Scala ArrayBuffer (syntax)

As a quick note, this is the syntax for creating a Scala ArrayBuffer:

import scala.collection.mutable.ArrayBuffer

val fruits = ArrayBuffer[String]()
val ints = ArrayBuffer[Int]()

The key thing to know is that the keyword new is not required before the ArrayBuffer. (This is because ArrayBuffer is either defined as a case class, or because it has an apply method defined. I haven’t looked at its source code to know which approach is taken.)

How to set uninitialized var fields (field types) in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 4.9, “How to set uninitialize var field types in Scala.”

Problem

You want to set the type for an uninitialized var field in a Scala class, so you begin to write code like this:

var x =

and then wonder how to finish writing the expression.

How to restore an Android emulator to its initial "factory" settings (wipe the data)

To restore an Android emulator to its initial, default settings, you need to find its “image” file on your filesystem and delete it. The image file will be found in your $HOME/.android/avd/<avdname>.avd folder.

For example, I was just working with an AVD named Nexus_6_API_21, and its directory on my Mac OS X system is /Users/al/.android/avd/Nexus_6_API_21.avd. I moved into that directory and found several “.img” files, deleted them, and then restarted the emulator, and it went back to its default settings.

Initialize Scala variables with Option, None, and Some (not null)

Summary: How to properly use the Scala Option/Some/None idiom to initialize empty var fields -- and specifically how not to use null values for the same purpose.

When you get started in the Scala world, you quickly learn that null values are a bad thing. Scala makes it easy to replace null values with something better, and that something better is what I call the Option/Some/None pattern (or idiom).