I know this must be a duplicated question, but I had to ask because I can't understand why my code doesn't work. I get this error:
Index was out of range. Must be non-negative and less than the size of the collection
Here is the part of my code that throws an exception:
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];
}
}
Please give me an answer. I don't know what am I supposed to do. It throws the exception above mentioned. Problem is that it throws at arraylist1[0]
position. why is that? How to solve that?
here the values:
"4/28/2008","4/29/2008",5/10/2008 likewise 33 values