Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在kotlin中正确迭代数组_Kotlin_Data Structures - Fatal编程技术网

如何在kotlin中正确迭代数组

如何在kotlin中正确迭代数组,kotlin,data-structures,Kotlin,Data Structures,我目前正在学习kotlin,因此在练习中遵循kotlin轨道。下面需要我计算两个字符串之间的汉明差(因此基本上只是计算差的数量) 我使用以下代码获得了解决方案: object Hamming { fun compute(dnaOne: String, dnaTwo: String): Int { if (dnaOne.length != dnaTwo.length) throw IllegalArgumentException("left and right strand

我目前正在学习kotlin,因此在练习中遵循kotlin轨道。下面需要我计算两个字符串之间的汉明差(因此基本上只是计算差的数量)

我使用以下代码获得了解决方案:

object Hamming {
    fun compute(dnaOne: String, dnaTwo: String): Int {
        if (dnaOne.length != dnaTwo.length) throw IllegalArgumentException("left and right strands must be of equal length.")
        var counter = 0
        for ((index, letter) in dnaOne.toCharArray().withIndex()) {
            if (letter != dnaTwo.toCharArray()[index]) {
                counter++
            }
        }
        return counter
    }
}
然而,在一开始,我尝试执行
dnaOne.split(“”).withIndex()
而不是
dnaOne.tocharray().withIndex()
,后者不起作用,它会在第一次迭代和下面的示例后停止
Hamming.compute(“GGACGGATTCTG”、“AGGACGGATTCT”)
将返回1,而不是正确的整数9(只有在使用
tocharray
时才会返回)


如果有任何解释,我将不胜感激。

我可以通过使用内置函数来简化此过程,因为String
在Kotlin中实现了
CharSequence`

根据
zip
的文档:

返回由
字符和具有相同索引的[其他]字符序列生成的对的列表

返回的列表具有最短字符序列的长度

这意味着我们将得到一个
列表
(相同位置的成对字母列表)。现在我们有了这个,我们可以用它来确定其中有多少是不同的

我将其实现为
字符串
上的扩展函数,而不是
对象

fun String.hamming(other: String): Int =
    if(this.length != other.length) {
       throw IllegalArgumentException("String lengths must match")
    } else {
       this.zip(other).count { it.first != it.second }
    }
这也成为了一个单一的表达现在

并称之为:

val ham = "GGACGGATTCTG".hamming("AGGACGGATTCT")
println("Hamming distance: $ham")

我能够通过使用内置函数来简化这个过程,因为String
在Kotlin中实现了
CharSequence`

根据
zip
的文档:

返回由
字符和具有相同索引的[其他]字符序列生成的对的列表

返回的列表具有最短字符序列的长度

这意味着我们将得到一个
列表
(相同位置的成对字母列表)。现在我们有了这个,我们可以用它来确定其中有多少是不同的

我将其实现为
字符串
上的扩展函数,而不是
对象

fun String.hamming(other: String): Int =
    if(this.length != other.length) {
       throw IllegalArgumentException("String lengths must match")
    } else {
       this.zip(other).count { it.first != it.second }
    }
这也成为了一个单一的表达现在

并称之为:

val ham = "GGACGGATTCTG".hamming("AGGACGGATTCT")
println("Hamming distance: $ham")

您是否在当前使用的两个位置都使用了
split(“”
)?是的。使用
split(“”)
它是类
class java.util.ArrayList
ToCharray()
将字符串转换为
class[C
的一个类,我只是不明白这对迭代它有什么影响尝试一下,我得到了预期的9看起来就像您使用
dnaOne.split(“”.withIndex()
时一样?旁注:在这两种情况下,您都在
dnaTwo
上调用
split
ToCharray太多次了,请在循环之前只执行一次。您是否在当前使用
ToCharray()的两个位置都使用了
split(“”
?是的。使用
split(“”)
它是类
class java.util.ArrayList
,而
tocharray()
将字符串转换为
class[C
的一个类,我只是不知道这对它的迭代有什么影响尝试一下,我得到了预期的9看起来就像您使用
dnaOne.split(“”.withIndex()
时一样?旁注:在这两种情况下,您都在
dnaTwo
上调用
split
toCharArray太多次了,请在循环之前只调用一次。