为什么此代码会生成';索引超出范围';例外
本文关键字:范围 例外 索引 为什么 代码 | 更新日期: 2023-09-27 18:25:36
我知道这一定是一个重复的问题,但我不得不问,因为我不明白为什么我的代码不起作用。我得到这个错误:
索引超出范围。必须是非负的并且小于集合的大小
以下是我的代码中抛出异常的部分:
private static void getPattern(ArrayList arrayList1)
{
ArrayList array_2008 = new ArrayList();
ArrayList array_2009 = new ArrayList();
ArrayList array_2010 = new ArrayList();
ArrayList array_2011 = new ArrayList();
ArrayList array_2012 = new ArrayList();
for (int i = 0; i < arrayList1.Count; i++)
{
if (arrayList1[i].ToString().Contains("2008"))
{
int a = getIndex(arrayList1[i].ToString());
array_2008[a] = arrayList1[i];
}
if (arrayList1[i].ToString().Contains("2009"))
{
int a = getIndex(arrayList1[i].ToString());
array_2009[a] = arrayList1[i];
}
else if (arrayList1[i].ToString().Contains("2010"))
{
int a = getIndex(arrayList1[i].ToString());
array_2010[a] = arrayList1[i];
}
else if (arrayList1[i].ToString().Contains("2011"))
{
int a = getIndex(arrayList1[i].ToString());
array_2011[a] = arrayList1[i];
}
else if (arrayList1[i].ToString().Contains("2012"))
{
int a = getIndex(arrayList1[i].ToString());
array_2012[a] = arrayList1[i];
}
}
请给我一个答案。我不知道该怎么办。它抛出了上面提到的异常。问题是它在arraylist1[0]
位置抛出。为什么?如何解决?
这里的值:
"4/28/2008","4/29/2008",5/10/2008同样有33个值
var array_2008 = new List<object>() {0, string.Empty};
var array_2009 = new List<object>() {0, string.Empty};
var array_2010 = new List<object>() {0, string.Empty};
var array_2011 = new List<object>() {0, string.Empty};
var array_2012 = new List<object>() {0, string.Empty};
for (int i = 0; i < arrayList1.Count; i++)
{
var aIndex = getIndex(arrayList1[i].ToString());
if (arrayList1[i].ToString().Contains("2008"))
{
//array_2008[0] = aIndex;
//array_2008[1] = arrayList1[i]; uncomment this if you wanat to add List<T> other wise comment out bottom code
//and uncomment code above
array_2008.Add( new List<object>()
{
aIndex, arrayList1[i].ToString()
});
}
if (arrayList1[i].ToString().Contains("2009"))
{
array_2009.Add( new List<object>()
{
aIndex, arrayList1[i].ToString()
});
}
else if (arrayList1[i].ToString().Contains("2010"))
{
array_2010.Add(new List<object>()
{
aIndex, arrayList1[i].ToString()
});
}
else if (arrayList1[i].ToString().Contains("2011"))
{
array_2011.Add(new List<object>()
{
aIndex, arrayList1[i].ToString()
});
}
else if (arrayList1[i].ToString().Contains("2012"))
{
array_2012.Add(new List<object>()
{
aIndex, arrayList1[i].ToString()
});
}
}
您需要改变处理此问题的方式。您当前正在尝试获取参数数组中值的索引,并在目标数组中使用该索引。当然,这在空的ArrayList上是行不通的!
你需要做的是重新开始使用现代C#:
public static void GetPattern(List<string> values)
{
var years = new Dictionary<string, List<string>>();
foreach(var value in values)
{
if(value.Contains("2008"))
{
if(!years.ContainsKey("2008")) years.Add("2008", new List<string>());
years["2008"].Add(value);
}
if(value.Contains("2009"))
{
if(!years.ContainsKey("2009")) years.Add("2009", new List<string>());
years["2009"].Add(value);
}
// Add for each year you want to track.
}
// Do something with your pattern.
}
通过依赖List<T>
类并将FindAll()
与谓词一起使用,可以进一步简化方法。
private static void getPattern(List<string> inputData)
{
List<string> array_2011 = inputData.FindAll(data => data.Contains("2011"));
//...
}
您可以将List<T>
的泛型参数更改为ArrayList
中的任何参数,但在C#中使用Linq
比使用ArrayList
要容易得多(看起来您是从Java中获得的?)。您甚至可以将其扩展为与任何IEnumerable
集合兼容。对于一般object
情况:
private static void getPattern(List<object> inputData)
{
List<object> array_2011 = inputData.FindAll(data => data.ToString().Contains("2011"));
//...
}
这将避免对大小为0的集合进行迭代。
这是因为您试图在a
位置向数组列表(2008到2012的列表)添加一个值,但它们是空的,因此a
超出了范围。您需要使用数组列表的add函数,或者使用已设置为特定大小(可能与arrayList1
大小相同)的数组。希望这能帮助
由于ArrayLists为空,因此不存在索引。首先添加一些伪数据来创建索引,然后添加实际值。我希望这能解决你的问题,我已经测试过它正在工作
private static void getPattern(ArrayList arrayList1)
{
ArrayList array_2008 = new ArrayList();
ArrayList array_2009 = new ArrayList();
ArrayList array_2010 = new ArrayList();
ArrayList array_2011 = new ArrayList();
ArrayList array_2012 = new ArrayList();
for (int i = 0; i < arrayList1.Count; i++)
{
if (arrayList1[i].ToString().Contains("2008"))
{
int a = getIndex(arrayList1[i].ToString());
for (int j = 0; j < a + 1; j++)
{
array_2008.Add("empty");
}
array_2008[a] = arrayList1[i];
}
if (arrayList1[i].ToString().Contains("2009"))
{
int a = getIndex(arrayList1[i].ToString());
for (int j = 0; j < a + 1; j++)
{
array_2009.Add("empty");
}
array_2009[a] = arrayList1[i];
}
else if (arrayList1[i].ToString().Contains("2010"))
{
int a = getIndex(arrayList1[i].ToString());
for (int j = 0; j < a + 1; j++)
{
array_2010.Add("empty");
}
array_2010[a] = arrayList1[i];
}
else if (arrayList1[i].ToString().Contains("2011"))
{
int a = getIndex(arrayList1[i].ToString());
for (int j = 0; j < a + 1; j++)
{
array_2011.Add("empty");
}
array_2011[a] = arrayList1[i];
}
else if (arrayList1[i].ToString().Contains("2012"))
{
int a = getIndex(arrayList1[i].ToString());
for (int j = 0; j < a + 1; j++)
{
array_2012.Add("empty");
}
array_2012[a] = arrayList1[i];
}
}