我有一些代码,当它执行时,它抛出一个IndexOutOfRangeException,说,
指数数组的边界之外.
这是什么意思,我能做些什么呢?
根据使用的类别,它也可以 ArgumentOutOfRangeException
mscorlib.dll中出现"System.ArgumentOutOfRangeException"类型的异常但未在用户代码中处理附加信息:索引超出范围.必须是非负数且小于集合的大小.
我收到以下错误之一:
它是什么意思,我该如何解决?
为什么List<T>.IndexOf允许超出范围的起始指数?
var list = new List<int>() { 100 };
Console.WriteLine(list.IndexOf(1/*item*/, 1/*start index*/));
Run Code Online (Sandbox Code Playgroud)
没有任何例外.但是1这个系列中没有带索引的项目!只有一个带0索引的项目.那么,为什么.Net允许你这样做呢?
确切的错误:
指数超出范围.必须是非负数且小于集合的大小.
我已经无数次索引数组和列表.我用过数组循环和列表无数次.数据在那里,它的工作原理.除非我尝试为我的功能创建任务.请注意,我成功地使用foreach循环执行此类功能; 这个新的需要两个参数,所以我不能正确使用foreach循环.至少我认为我不能.
这是错误的代码:
if (addressList != null) {
textBox1.Text += ("Address List Length: " + addressList.Count + Environment.NewLine);
for (int i = 0; i < addressList.Count; i++) {
textBox1.Text += ("Task for " + addressList[i] + ":" + portList[i] + " initiated." + Environment.NewLine);
Task.Factory.StartNew(() => PingTaskAdapted(addressList[i], portList[i]));
}
}
else textBox1.Text = ("No IPs have been added.");
Run Code Online (Sandbox Code Playgroud)
假设addressList[0]是google.com并且portList[0]是80,输出:
Address List Length: 1
Task for google.com:80 initiated.
Run Code Online (Sandbox Code Playgroud)
然后程序中断,Visual Studio告诉我在PingTaskAdapted()中我正在调用一个超出范围的索引,当它只是打印出有问题的索引时,因为它们存在.
而且要明确的是,如果我称之为PingTaskAdapted(addressList[0], pingList[0]);没有问题.
c# lambda task indexoutofboundsexception indexoutofrangeexception
当我使用索引器初始化数组和访问元素时,这很好用:
object[] temp = new object[5];
temp[0] = "bar";
Run Code Online (Sandbox Code Playgroud)
现在我希望同样适用于a List<T>,因为你可以通过将容量传递给构造函数来初始化它:
List<object> temp = new List<object>(5);
temp[0] = "bar";
Run Code Online (Sandbox Code Playgroud)
但是,最后一行会引发以下异常:
指数超出范围.必须是非负数且小于集合的大小
为什么这种情况发生在List<T>类型上,而不是阵列?由于数组只是CLR集合的低级抽象,那么为什么会发生这种异常呢?
因此??,当左手为null时,我们必须解析其右手值。什么是等价的string[]。
例如
string value = "One - Two"
string firstValue = value.Split('-')[0] ?? string.Empty;
string secondValue = value.Split('-')[1] ?? string.Empty;
Run Code Online (Sandbox Code Playgroud)
如果我们尝试获取第三个索引或,上述示例仍然会崩溃string value = "One"。因为它不是null而是IndexOutOfRangeException被抛出。
https://docs.microsoft.com/zh-cn/dotnet/api/system.indexoutofrangeexception
那么解决上述问题的单线解决方案是什么?我想避免尝试捕获的情况,因为这会产生难看的代码。
我想string[]使用a string.Empty作为备用值来获取价值,所以我string永远不会null。
我有以下代码用于查找给定切片中给定总和的两个整数:
type Store_object struct {
C int
I int
Prices []int
}
//..other unrelated functions...
func FindItemPairs(scenarios []Store_object) ([]string, error) {
var results []string
for scIndex := 0; scIndex < len(scenarios); scIndex++ {
scenario := scenarios[scIndex]
for prIndex := 0; prIndex < len(scenario.Prices); prIndex++ { //<--!sc
firstItem := scenario.Prices[prIndex]
if firstItem >= scenario.C {
continue
}
for cmpIndex := prIndex + 1; cmpIndex < len(scenario.Prices); cmpIndex++ {
secondItem := scenario.Prices[cmpIndex]
switch {
case secondItem >= scenario.C:
continue
case firstItem+secondItem == …Run Code Online (Sandbox Code Playgroud) 我在向通用字典 (WinRT C#) 添加值时遇到 IndexOutOfRange 异常。下面是我的代码和异常的堆栈跟踪。
代码:
if (!data.TryGetValue(index, out cells))
{
cells = new CellCollection();
data.Add(index, cells);
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
在 System.Collections.Generic.Dictionary
2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary2.Add(TKey 键,TValue 值)
我不希望在添加时发生这种超出范围的异常。我的应用程序仅使用单线程。我已将值添加到字典中,并在不需要时使用 GC.Collect() 将其从字典中删除。然后根据需要添加该值。
任何人都可以提出解决此异常的建议吗?
c# generics dictionary windows-runtime indexoutofrangeexception
我必须以下列格式从用户那里获取输入,并从中创建一个嵌套列表.第一行是行数.
3
Sourav Das 24 M
Titan Das 23 M
Gagan Das 22 F
Run Code Online (Sandbox Code Playgroud)
嵌套列表应该是:
parentlist = [
['Sourav', 'Das', '24', 'M']
['Titan', 'Das', '23', 'M']
['Gagan', 'Das', '22', 'M']
]
Run Code Online (Sandbox Code Playgroud)
我写了以下代码:
k = int(raw_input())
parentlist = [[]]
for i in range(0, k):
str1 = raw_input()
parentlist[i] = str1.split()
Run Code Online (Sandbox Code Playgroud)
但是在进入第二行后它会给出一些索引超出范围的异常(如下所示).它给出此异常的代码有什么问题?
3
Sourav Das 24 M
Titan Das 23 M
Traceback (most recent call last):
File "nested.py", line 5, in <module>
parentlist[i] = str1.split()
IndexError: list assignment index out of range …Run Code Online (Sandbox Code Playgroud) python user-input nested-lists python-2.7 indexoutofrangeexception
我有一个代码,允许用户选择一个文件,将一些有关它的信息添加到可观察的集合中并显示上传进度,完成后,它将图像绑定到图像视图。它立即可以正常工作,但如果重复该过程,则会引发异常:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.43(intptr,intptr)
at at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.43(intptr,intptr)
Run Code Online (Sandbox Code Playgroud)
插入新元素并获取其索引的代码
switch(x){
// some code...
case 6:
// .... some code
_ = Task.Run(async () => {
try
{
int i = default;
Msg newmsg2 = new Msg() { UploadProgress = 0.0, UploadProgressVisibility = true, IsImageSend = true, ImageSend = "@drawable/icon_default" };
valuesLock.EnterWriteLock();
try
{
// THE ISSUE IS …Run Code Online (Sandbox Code Playgroud) c# collections observablecollection xamarin indexoutofrangeexception
c# ×8
.net ×3
arrays ×2
list ×2
collections ×1
dictionary ×1
generics ×1
go ×1
lambda ×1
nested-lists ×1
python ×1
python-2.7 ×1
string ×1
task ×1
user-input ×1
xamarin ×1