gpt4 book ai didi

Does anyone know how to use swap() method now sn kotlin? Because it shows me an error that there is no such method(Sn Kotlin现在有人知道如何使用swap()方法吗?因为它向我显示了一个错误,即没有这样的方法)

转载 作者:bug小助手 更新时间:2023-10-28 10:49:11 34 4
gpt4 key购买 nike



Does anyone know how to use swap() method now sn kotlin? Because it shows me an error that there is no such method.

Sn Kotlin现在有人知道如何使用swap()方法吗?因为它向我展示了一个错误,即没有这样的方法。


enter image description here

在此输入图像描述


Tried to copy this code in mine. Saw a lot of examples how to rewrite this, but this is an exercise on how to search mistakes (here it is in the size of the list and if loop), but now instead of one mistake that i need i have 2

我试着把这个代码复制到我的电脑里。我看到了很多如何重写它的例子,但这是一个关于如何搜索错误的练习(这里是列表和if循环的大小),但现在我有2个错误,而不是我需要的一个错误


Updated by moderator:

版主更新:


Author of the question provided this code in the comment:

问题的作者在评论中提供了以下代码:


fun main() {
val list = listOf(5, 3, 6, 7, 9, 1)
sortlist(list)
println(list)
}

private fun sortlist(list: List<Int>) {
for (i in 0..list.size -1) {
for(j in 0.. list.size - 2) {
if (list[j] > list[j + 1] ) {
Collection.swap(list, j, j + 1)
}
}
}
}

更多回答

Please paste the text of your own code that you tried into your question. Text, not a picture, please.

请将您尝试的代码的文本粘贴到您的问题中。请给我发短信,不要图片。

Even in your picture there is no red underline for swap(). Does it show an error when you try to build the application? What is the error exactly?

即使在您的图片中,也没有交换()的红色下划线。当您尝试构建应用程序时,它是否显示错误?具体的错误是什么?

@broot they said that’s the code they were trying to copy.

@布罗特,他们说这就是他们试图复制的代码。

Maybe you forgot to import java.util.Collections?

也许您忘记了导入java.util.Colltions?

@broot val list = listOf(5, 3, 6, 7, 9, 1) sortlist(list) println(list) } private fun sortlist(list: List<Int>){ for (i in 0..list.size -1){ for(j in 0.. list.size - 2){ if (list[j] > list[j + 1] ){ Collection.swap(list, j, j + 1) } } } }

@BROOT Val list=list of(5,3,6,7,9,1)sortlist(List)println(List)}Private Fun sortlist(list:list){for(i in 0..list.size-1){for(j in 0..List.Size-2){if(List[j]>List[j+1]){Collection.swap(List,j,j+1)}}}

优秀答案推荐

Apparently, you made a mistake when copying the code. In the original code included in the image, the function name was: Collections.swap() which is the correct name. In the code you provided in the comments it is: Collection.swap(). You miss a letter "s" and this is probably the cause of your problem.

显然,你在复制代码时犯了个错误。在图像中包含的原始代码中,函数名称是:Collections.swap(),这是正确的名称。在您在注释中提供的代码中,它是:Collection.swap()。你漏了一个字母“s”,这可能是你的问题的原因。



Collections.swap(mutableList,i,j) 

method requires the mutable list in order to swap it's elements. Make sure you provide the mutable list to this method. As per your code, it's looking you are passing immutable list.

方法需要可变列表才能交换其元素。确保将可变列表提供给此方法。根据您的代码,它看起来您正在传递不可变的列表。


更多回答

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com