如果在Kotlin中指定了某些大小,如何为数据类设置值? [英] How to set values to data class if some size is specified in kotlin?

查看:115
本文介绍了如果在Kotlin中指定了某些大小,如何为数据类设置值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 kotlin 中有一个数据类,像这样:

I have a data class in kotlin like this:

data class myDataClass(val myArr: ArrayList<Char>)

现在,假设我创建它的一个实例,如下所示:

Now, suppose I create an instance of it as follows:

val myData = myDataClass(x)    // x is an integer; 1 <= x <= 9

我想要 myData 应该具有以下数据:

I want that myData should have the following data:

println(myData.myArr)
// [A, B, C, D, ...]


推荐答案

可能:

data class myDataClass(val myArr: ArrayList<Char>) {
    constructor(i: Int) : this(ArrayList((0..i).map { ('A' + it).toChar() }))
}

但是事实是,这是一个非常奇怪的代码

But the truth is, it's a pretty strange code

这篇关于如果在Kotlin中指定了某些大小,如何为数据类设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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