0

I am using a function to create a 6 element Boolean Array. I am using the array.component1() function in an if statement to turn the color of a textView object Green. Seems to work great for calling .component 1 thru 5, but I get an error trying to call array.component6(), and I don't understand why.

internal var endArray = setEnd(totLine, totTurn, startTurn)

        resultTextView1.text = "A"
            if (endArray.component1()) resultTextView1.setTextColor(Color.GREEN)
        resultTextView2.text = "B"
            if (totLine<2) resultTextView2.visibility = View.GONE
            if (endArray.component2()) resultTextView2.setTextColor(Color.GREEN)
        resultTextView3.text = "C"
            if (totLine<3) resultTextView3.visibility = View.GONE
            if (endArray.component3()) resultTextView3.setTextColor(Color.GREEN)
        resultTextView4.text = "D"
            if (totLine<4) resultTextView4.visibility = View.GONE
            if (endArray.component4()) resultTextView4.setTextColor(Color.GREEN)
        resultTextView5.text = "E"
            if (totLine<5) resultTextView5.visibility = View.GONE
            if (endArray.component5()) resultTextView5.setTextColor(Color.GREEN)
        resultTextView6.text = "F"
            if (totLine<6) resultTextView6.visibility = View.GONE
            if (endArray.elementAt(6)) resultTextView6.setTextColor(Color.GREEN) //NOT WORKING

fun setEnd(totLine: Int, totTurn: Int, startTurn: Int): Array<Boolean> {
    //internal var totLine: Int = 4 // total no of lines
    //internal var totTurn: Int = 2 // no of turns
    //internal var startTurn: Int = 1 // which turn starts first
    var textA = false
    var textB = false
    var textC = false
    var textD = false
    var textE = false
    var textF = false
    var totLtr: Int = totLine / totTurn // no archers per line

    if(startTurn==1) {
        if (totLtr == 1) textA = true
        if (totLtr == 2) {
            textA = true
            textB = true
        }
        if (totLtr == 3) {
            textA = true
            textB = true
            textC = true
        }
    }
    if(startTurn==2) {
        if (totLtr == 1) textB = true
        if (totLtr == 2) {
            textC = true
            textD = true
        }
        if (totLtr == 3) {
            textD = true
            textE = true
            textF = true
        }
    }
    if(startTurn==3) {
        if (totLtr == 1) textC = true
        if (totLtr == 2) {
            textE = true
            textF = true
        }
    }

    return arrayOf(textA, textB, textC, textD, textE, textF)
    }

3 Answers 3

3

The componentN() functions are 1-based: the first component is component1(), the second is component2()

That differs from arrays, lists, and most other linear structures, which are 0-based: the first item has index 0, the second has index 1…

That's because the componentN() functions aren't really intended for accessing arrays or lists.  They're for accessing individual properties of data classes, Pairs, map entries, and other cases where they can give easy access to a few values in a destructuring declaration.  Arrays do have componentN() functions, but only for the first 5 elements.  That's probably to let you do things like easy string splitting:

val (key, value) = line.split("=")

So there are two problems here.  First, as you've probably discovered, there's no component6() function defined on arrays, only component1–5.  And second, if you switch to elementAt(), that's using the array index and so is 0-based, so elementAt(6) tries to get the seventh element — which doesn't exist, hence the exception you're getting.

But the underlying problem is that you're making it far harder than necessary!

If you need to access an array (or list) element by index, you can simply use the standard square-bracket notation:

endArray[5]

Also, all those repetitive lines are a strong code smell!  (They're also a sign that you haven't posted a complete, minimal, verifiable example…)

Instead of hard-coding for each element separately, there's almost certainly a much shorter way using a loop or similar.  (Consider how you'd write this if there were 30 elements instead of 6?  Or 1000?  Or an unknown number?)

Sign up to request clarification or add additional context in comments.

1 Comment

ya, I am a rookie programmer. My first attempt is to get the code laid out to work, and then I will optimize it with loops, etc. to minimize programming.
0

I refactored your code

internal var endArray = setEnd(totLine, totTurn, startTurn)

val textViews = arrayOf<TextView>(
    resultTextView1,
    resultTextView2,
    resultTextView3,
    resultTextView4,
    resultTextView5,
    resultTextView6
)

for ((index, char) in ('A'..'F').withIndex()) {
    textViews[index].apply {
        isVisible = totLine > index + 1
        text = char.toString()
        if (endArray[index]) setTextColor(Color.GREEN)
    }
}

fun setEnd(totLine: Int, totTurn: Int, startTurn: Int): BooleanArray {
    //internal var totLine: Int = 4 // total no of lines
    //internal var totTurn: Int = 2 // no of turns
    //internal var startTurn: Int = 1 // which turn starts first
    val totLtr: Int = totLine / totTurn // no archers per line
    val text5And6 = startTurn + totLtr == 5
    return booleanArrayOf(
        startTurn == 1,
        startTurn == 1 && totLtr > 1 || startTurn == 2 && totLtr == 1,
        startTurn + totLtr == 4,
        startTurn == 2 && totLtr > 1,
        text5And6,
        text5And6,
    )
}

1 Comment

wow. very nice code all around! fixed a couple syntax errors, but really clean. Thanks!
0

The componentN functions are intended only for destructuring declarations, and arrays/collections only define them up to five components. It would be messy (poor code readability) to destructure more than that.

You're using these functions in an unintended way. You should use array access syntax (brackets []) to get items out of an array by index.

2 Comments

I'm a newbee to Kotlin. Can you provide a simple example?
I don't think I can explain it clearer than the page I linked. But I didn't notice that you also skipped the sixth element. The other answer has a better explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.