0
votes

I have the following two dimensional array:

 val arr = Array(3){column-> IntArray(5){it+column} }

i want to set the value 5 in the place arr[1][3] (index 1,3) so when i write the following code:

 arr[1,3] = 5

I'm getting the following error:

The integer literal does not conform to the expected type IntArray

When trying to use IDE fix (Alt+Shift+Enter) it creates the following function:

private operator fun <T> Array<T>.set(i: Int, unit: Unit, value: Int)

Now i don't understand the meaning of the unit variable and don't know how implement this function

1
I don't quite get it. Why don't you just use the syntax arr[1][3] = 5 instead of the comma? You don't need any custom operator for this - Joffrey
@Joffrey, just for learning purpose mate - DorVak
Ah I see, then yes it's just about the incorrect print statement, with parentheses instead of curly braces ;) - Joffrey

1 Answers

0
votes

Your code works fine for me, with your version of the set operator!

You need a getter too, if you want to use the same syntax:

private operator fun Array<IntArray>.get(i: Int, j:Int) = get(i).get(j)

And your string code needs curly braces, when you're wrapping an expression:

println("--------${arr[1][3]}")

Not sure about the unit parameter in the auto-generated code, that's a weird one