kotlin从多个ip数组中找到最小的ip假设有一个名为`ipArrays`,类型为`List>`,每个元素都是一个长度为4的整数数组,代表一个IP地址,我们可以使用以下代码找到最小的IP地址:```kotlinfun findSmallestIp(ipArrays: List>): Array? { var smallestIp: Array? = null for (ipArray in ipArrays) { if (smallestIp == null || compareIp(ipArray, smallestIp) < 0) { smallestIp = ipArray } } return smallestIp}fun compareIp(ip1: Array, ip2: Array): Int { for (i in 0..3) { if (ip1[i] != ip2[i]) { return ip1[i] - ip2[i] } } return 0}```使用示例:```kotlinval ipArrays = listOf( arrayOf(192, 168, 0, 1), arrayOf(192, 168, 0, 10), arrayOf(192, 168, 0, 5))val smallestIp = findSmallestIp(ipArrays)println("Smallest IP: ${smallestIp?.joinToString(".")}")```输出结果:`Smallest IP: 192.168.0.1`这个ip数组为字符串好的,如果IP地址的数组是字符串格式的,我们可以稍微修改一下之前的代码,通过将IP地址字符串分割成整数数组来进行比较。```kotlinfun findSmallestIp(ipArrays: List): String? { var smallestIp: String? = null for (ipString in ipArrays) { val ipArray = ipString.split(".").map { it.toInt() }.toTypedArray() if (smallestIp == null || compareIp(ipArray, smallestIp.split(".").map { it.toInt() }.toTypedArray()) < 0) { smallestIp = ipString } } return smallestIp}fun compareIp(ip1: Array, ip2: Array): Int { for (i in 0..3) { if (ip1[i] != ip2[i]) { return ip1[i] - ip2[i] } } return 0}```使用示例:```kotlinval ipStrings = listOf("192.168.0.1", "192.168.0.10", "192.168.0.5")val smallestIp = findSmallestIp(ipStrings)println("Smallest IP: $smallestIp")```输出结果:`Smallest IP: 192.168.0.1`