0

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

I want that myData should have the following data:

println(myData.myArr)
// [A, B, C, D, ...]
Akshdeep Singh
  • 1,301
  • 1
  • 19
  • 34
  • It's hard to suppose you create an instance of the class like that when it results in a compilation error.... – Clay07g Aug 15 '19 at 18:24
  • @Clay07g sorry for my bad english, I meant that I should be able to create an instance of the class like that so that it should give me the desired output. – Akshdeep Singh Aug 15 '19 at 19:20

1 Answers1

1

It's possible:

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

Andrei Tanana
  • 7,932
  • 1
  • 27
  • 36