karim
karim

Reputation: 63

How to initialize an array of classes in kotlin?

I get an error when I put the type and size of an array of classes

I have tried:

fun main(args :Array<String>) {

    class modul() {
        var nommodul: String? = null
        var coeff: Int? = null
        var note: Int? = null
    }

    var releve

    class notes() {
        var releve: array<modul>(10){""} here the erreur 


    }
}

Upvotes: 3

Views: 9486

Answers (2)

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28228

First of all, your code has several errors. This might be an MCVE and/or copy-paste issue, but I need to address these before I get started on the arrays.

var releve before the notes class isn't allowed. You don't assign it, you don't declare a type, and the compiler will complain if you copy-paste the code from your question.

Second, the array var itself: Array is upper-case, and initialization is separate. This would be more valid (note that this still does not work - the solution for that comes later in this answer):

var releve: Array<modul> = Array(10) {...}
// or
var releve = Array<modul>(10) {...}

And the last thing before I start on the array itself: please read the language conventions, especially the naming ones. Your classes should all start with an upper-case letter.


Kotlin arrays are quite different from Java arrays in many ways, but the most notable one being that direct array initialization also requires an initializer.

The brackets are expected to create a new instance, which you don't. You create a String, which isn't, in your case, a modul.

There are several ways to fix this depending on how you want to do this.

If you have instances you want to add to the array, you can use arrayOf:

arrayOf(modulInstance, modulInstance2, ...)

If you want to create them directly, you can use your approach:

 var releve = Array(10) { modul() } 

A note about both of these: because of the initialization, you get automatic type inference and don't need to explicitly declare <modul>


If you want Java-style arrays, you need an array of nulls.

There's two ways to do this:

var releve = arrayOfNulls<modul>(10)
// or
var releve = Array<modul?>(10) { null }

I highly recommend the first one, because it's cleaner. I'm not sure if there's a difference performance-wise though.

Note that this does infer a nullable type to the array, but it lets you work with arrays in a similar way to Java. Initialization from this point is just like Java: releve[i] = modul(). This approach is mostly useful if you have arguments you want to add to each of the classes and you need to do so manually. Using the manual initializers also provides you with an index (see the documentation) which you can use while initializing.

Note that if you're using a for loop to initialize, you can use Array(10) { YourClass() } as well, and use the supplied index if you need any index-sensitive information, such as function arguments. There's of course nothing wrong with using a for loop, but it can be cleaner.


Further reading

Upvotes: 4

Koushik Mondal
Koushik Mondal

Reputation: 875

here some example of kotlin array initialization:

array of Library Method

val strings = arrayOf("January", "February", "March")

Primitive Arrays

val numbers: IntArray = intArrayOf(10, 20, 30, 40, 50)

Late Initialization with Indices

val array = arrayOfNulls<Number>(5)

for (i in array.indices) {
    array[i] = i * i
}

See Kotlin - Basic Types for details

Upvotes: -1

Related Questions