我需要一个程序来反转两个终端之间列表的一部分。 示例:
列表: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/