c# - 反转列表的特定部分 C#

标签 c# list loops panel

我需要一个程序来反转两个终端之间列表的一部分。 示例:

列表:1、2、3、3、5、4 输出:1, 2, 3, 3, 4, 5(只有4和5倒过来)

我发现了这个:

                positionCrepe.Reverse(indexOfMaxToSearch, positionCrepe.Count);

但它不起作用,因为我有一个错误:

System.ArgumentException: The offset and length were out of bounds for this table or the number is greater than the number of index elements at the end of the source collection.

但是

indexOfMaxToSearch = 2

positionCrepe.count = 5

所以它不超过表的索引

谁有解决办法? 谢谢。

最佳答案

第二个参数是你想反转多少个元素,而不是列表中有多少个元素。

因此,如果您想反转从 indexOfMaxToSearch 开始的所有内容,您需要反转 positionCrepe.Count - indexOfMaxToSearch 元素:

positionCrepe.Reverse(indexOfMaxToSearch, positionCrepe.Count - indexOfMaxToSearch);

错误消息实际上是说第一个参数加上第二个参数超出了数组的范围。

关于c# - 反转列表的特定部分 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59273008/

相关文章:

c# - 如何修复 : NUnit unit tests not showing up in testing pad in Visual Studio Community (Mac)

C# 按字母顺序对字符串数组进行排序,注意将以大写字母开头的字符串放在前面。第一的

c# - 必需属性不适用于外键下拉列表

c# - 在 Entity Framework 中使用 Join、Group By 和 Sum

java - 在 Java 中选择 ArrayList 中最后 25% 的对象

performance - 为什么循环总是编译成 “do…while”样式(尾跳)?

php - mysql 连接三个表并分组

java - Android列表(ArrayList、List等): addAll() vs redefining reference

python - 如何获取不同单元格范围内的值?

javascript - 在javascript中循环和存储不同长度的obj