Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing arrays in kotlin

I am making a haiku generator in kotlin but I am getting a weird 'variable firstLineArray must be initialized' on this block of code.

import java.util.Random

fun oneSyllableWords() : String{
    val oneSyllableWordArray = arrayOf("Life", "Love", "One", "On", "No", "Go")
    val random = Random()
    val randint = random.nextInt(oneSyllableWordArray.size)
    return oneSyllableWordArray[randint]
}

fun main(args:Array<String>){
    var firstLineArray : Array<String>
    var syllablesRemaining = 5
    while(syllablesRemaining<0) {
        val random = Random()
        val randint = random.nextInt(4) + 1
        for (i in 0..4){
            if(randint == 1){
                firstLineArray[i] = oneSyllableWords()
            }
        }
    }
}
like image 682
Brandon Dodds Avatar asked May 29 '17 14:05

Brandon Dodds


People also ask

How does Kotlin Initialize array size?

In Kotlin, creating an IntArray of size N is simple. Use IntArray(n) or the appropriate type, as detailed thoroughly in hotkey's answer. In this case, x will be taken from index 0, y from index 1, etc.

How to initialize primitive arrays in Kotlin?

Kotlin offers specialized classes to represent arrays of primitive types such as IntArray, DoubleArray, LongArray, etc. To initialize primitive arrays with a specific value, you can use the class constructor with lambda. For example, IntArray(5)creates an integer array of size 5and initializes it with a value of 1. 1 2 3

How to access all the items in a Kotlin array?

All the items stored in a Kotlin array are called elements that can be accessed with the help of their index numbers starting from 0. Using the arrayOf () function and array constructor.

How to convert enumerated values to an array in Kotlin?

Kotlin has a built-in arrayOf method that converts the provided enumerated values into an array of the given type: 3. Primitive Arrays We can also use the arrayOf method with primitive values. However, Kotlin will autobox the primitive values to their corresponding object wrapper classes which will have detrimental performance implications.

How to avoid overhead in Kotlin programming?

To avoid this overhead Kotlin has wide support for primitive arrays. There are dedicated arrayOf methods for the following types: double, float, long, int, char, short, byte, boolean. We can easily initialize a primitive int array using its dedicated arrayOf method: 4. Late Initialization With Indices


3 Answers

You have to initialize your variable with a value. Arrays are fixed size containers, if I'm right that you need an array with a length of 5 here, you can use the following to create it with 5 empty strings as a start:

var firstLineArray: Array<String> = Array(5) { "" }

Or if you're okay with having an Array<String?> and having to deal with possible null values when reading from the array, you can do:

var firstLineArray: Array<String?> = arrayOfNulls(5)
like image 98
zsmb13 Avatar answered Nov 14 '22 01:11

zsmb13


you forget init the array:

Ways:

var firstLineArray = Array<String>()

or

var firstLineArray = Array (0, { i -> "" })

or

var firstLineArray = emptyArray<String>()
like image 37
A Monad is a Monoid Avatar answered Nov 14 '22 01:11

A Monad is a Monoid


You can initialize a String array by the below code:

var firstLineArray = arrayOfNulls<String>(5)

The above code will give a String array containing null. It returns Array<String?>

var firstLineArray = Array<String>(5) { "it = $it" } // returns Array<String>

var firstLineArray = arrayOf("a", "b", "c", "d", "e") // returns Array<String>
like image 31
Avijit Karmakar Avatar answered Nov 14 '22 02:11

Avijit Karmakar