Car车载开发之Sensor Logic篇排序优化篇

针对此篇博客的代码,说实话,太多了,不够简洁,而且非常麻烦,耦合非常严重,我这边优化了一版,用两个for循环实现。
Car车载开发之Sensor Logic篇

package com.fca.uconnect.assistdrivingpage.data

import com.fca.uconnect.assistdrivingpage.util.Constants

class TestOrder {

    companion object {

        val indexArray: IntArray = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

        val posArray: IntArray = intArrayOf(Constants.POSITION_FRR_01,
            Constants.POSITION_FRR_02,
            Constants.POSITION_RFRR_01,
            Constants.POSITION_WCR_02,
            Constants.POSITION_RMCR,
            Constants.POSITION_LFRR_02,
            Constants.POSITION_FRR_03,
            Constants.POSITION_WCR_03,
            Constants.POSITION_RFRR_02,
            Constants.POSITION_WCR_01,
            Constants.POSITION_LMCL,
            Constants.POSITION_LFRR_01)

        var changePosArray: Array<Int?> = arrayOfNulls<Int>(12)
        var sensorList: MutableList<SensorStateOnlyPosition> = mutableListOf()

        fun getCurrentListOrder(sensorPos: Int): MutableList<SensorStateOnlyPosition> {
            val current: Int = posArray.indexOf(sensorPos)
            println("current posArray:$current")
            val changePosArrayResult = getChangePosArray(current)
            for (i in indexArray.indices) {
                sensorList.add(SensorStateOnlyPosition(indexArray[i], changePosArrayResult[i]!!))
            }
            return sensorList
        }

        fun getChangePosArray(current: Int): Array<Int?> {
            changePosArray[0] = posArray[current]
            var startPoint = 0
            for (i in 0 until posArray.size + 1) {
                startPoint = (indexArray.size + 1 - i)
                if (current + i == indexArray.size) {
                    for (j in 0 until indexArray.size - 1) {
                        if (i != 1) {
                            if (startPoint == indexArray.size - 1) {
                                changePosArray[j + 1] = posArray[startPoint]
                                startPoint = 0
                            } else {
                                changePosArray[j + 1] = posArray[startPoint]
                                startPoint += 1
                            }
                        } else {
                            if (i == 1) {
                                changePosArray[j + 1] = posArray[j]
                                println("current index j:$j")
                            }
                        }
                    }
                } else {
                    continue
                }
            }
            return changePosArray
        }

        @JvmStatic
        fun main(args: Array<String>) {
            val testPos: Int = Constants.POSITION_LFRR_01
            val currentListOrder: MutableList<SensorStateOnlyPosition> =
                getCurrentListOrder(testPos)
            for (i in currentListOrder) {
                println("This is the order start from testPos :$testPos now index i:${i.position}")
            }
        }
    }
}

测试结果都是正确的:

This is the order start from testPos :12 now index i:12
This is the order start from testPos :12 now index i:1
This is the order start from testPos :12 now index i:2
This is the order start from testPos :12 now index i:3
This is the order start from testPos :12 now index i:4
This is the order start from testPos :12 now index i:5
This is the order start from testPos :12 now index i:6
This is the order start from testPos :12 now index i:7
This is the order start from testPos :12 now index i:8
This is the order start from testPos :12 now index i:9
This is the order start from testPos :12 now index i:10
This is the order start from testPos :12 now index i:11
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值