概述
针对此篇博客的代码,说实话,太多了,不够简洁,而且非常麻烦,耦合非常严重,我这边优化了一版,用两个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
最后
以上就是谨慎帅哥为你收集整理的Car车载开发之Sensor Logic篇排序优化篇的全部内容,希望文章能够帮你解决Car车载开发之Sensor Logic篇排序优化篇所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复