如何初始化Kotlin中的类数组? [英] How to initialize an array of classes in kotlin?

查看:1107
本文介绍了如何初始化Kotlin中的类数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入类数组的类型和大小时出现错误

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

我尝试过:

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 


    }
}

推荐答案

首先,您的代码有几个错误.这可能是MCVE和/或复制粘贴问题,但是在开始使用阵列之前,我需要解决这些问题.

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.您没有分配它,也没有声明类型,并且如果您从问题中复制粘贴代码,编译器会抱怨.

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.

第二,数组var本身:Array是大写的,并且初始化是独立的.这会更有效(请注意,这仍然行不通-该解决方案将在此答案后面提供):

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数组在很多方面与Java数组有很大不同,但是最值得注意的是直接数组初始化还需要一个初始化程序.

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.

应该使用方括号创建一个新实例,而您不会这样做.您创建的字符串(在您的情况下不是modul).

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.

如果有要添加到阵列的实例,则可以使用arrayOf:

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() } 

关于这两个方面的说明:由于初始化,您将获得自动类型推断,而无需显式声明<modul>

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

如果要使用Java样式的数组,则需要一个null数组.

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.

请注意,这确实会为数组推断可为null的类型,但是它使您可以以类似于Java的方式来处理数组.此时的初始化就像Java:releve[i] = modul().如果您有要添加到每个类的参数并且需要手动添加,则此方法最有用.使用手动初始化程序还会为您提供索引(请参阅文档),您可以在初始化时使用它.

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.

请注意,如果您使用for循环进行初始化,则也可以使用Array(10) { YourClass() },如果需要任何对索引敏感的信息(例如函数参数),则可以使用提供的索引.使用for循环当然没有错,但是它可以更干净.

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.

这篇关于如何初始化Kotlin中的类数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆