我定义了一个类如下:
public class PNT
{
public String Name;//点名
public double x, y, z;//已知点坐标、测站点精确坐标
public double x0, y0, z0;//近似坐标
public double mx, my, mz;//中误差
public double E, F, alpha;//误差椭圆
public bool isKnownPoint;//是否已知
public int ID;//点号
public PNT()
{
ID = -1;
isKnownPoint = false;
Name = "";
x = y = z = 0;
}
public PNT(PNT other)
{
Name = other.Name;
x = other.x;
y = other.y;
z = other.z;
x0 = other.x0;
y0 = other.y0;
z0 = other.z0;
mx = other.mx;
my = other.my;
mz = other.mz;
isKnownPoint = other.isKnownPoint;
ID = other.ID;
}
}
public class PntList
{
public int Capacity = 10000;//定义点的容量
public int Count; //实际点数;
public List<PNT> ets = new List<PNT>();
public PntList(int count)//:Count(count)
{
Count = count;
if (count == 0)
{
ets = new List<PNT>(Capacity);
}
else
{
Capacity = Count;
ets = new List<PNT>(Capacity);
}
}
public int Idxof(PNT A)
{
return Idxof(A.Name);
}
public int Idxof(String Name)//根据点名找到ID
{
for (int i = 0; i < Count; i++)
if (String.Equals(ets[i].Name, Name))//判断点名是否存在
return ets[i].ID;
return -1;
}
public bool Add(PNT A)
{
if (Count >= Capacity)//数量是否大于容量
return false;
if (Idxof(A) != -1)//所给点不存在,写入点列表
{
ets[Count] = A;//写入点
ets[Count].ID = Count;//分配点的ID,即点的序号
Count++;
return true;
}
else
{
return false;
}
}
然后又定义pntList实例 PntList tempList = new PntList( myarrs.Length );
通过文件流读取并往tempList中存数据,
for (int i = 0; i < myarrs.Length; i++)
{
cpinf = myarrs[i].Split(secondSplit, StringSplitOptions.RemoveEmptyEntries);
int tempSize = cpinf.Length;
tempList.ets[i].CountofDirt = (tempSize - 1) / 2;
}
myarrs是按行分割后的数组,cpinf 是又按","分割的数组tempList.ets[i].CountofDirt = (tempSize - 1) / 2;报错说是索引超限,
求大神,帮忙。谢谢了,感激不尽。
------解决思路----------------------
那就是ets[i]越界了,你跟一下,看看ets中够不够数量
------解决思路----------------------
PntList tempList = new PntList( myarrs.Length );
这儿实际上tempList 的count还是0;所以tempList [i]越界了
int[] arr = new int[5] { 1, 2, 3, 4, 5 };
Console.WriteLine("arr.Length" + arr.Length);
List<string> list2 = new List<string>(arr.Length);
Console.WriteLine("list2: " + list2.Count);
for (int i = 0; i < arr.Length; i++)
{
string srt = arr[i].ToString();
list2[i] = srt;
Console.Write(list2[i] + " ");
}
结果:
System.Int32[]
list2: 0
未经处理的异常: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值
并小于集合大小。
参数名: index
在 System.ThrowHelper.ThrowArgumentOutOfRangeException()
在 System.Collections.Generic.List`1.set_Item(Int32 index, T value)
在 ConsoleApplication18.Program.Main(String[] args) 位置 d:\用户目录\Document
s\Visual Studio 2012\Projects\ConsoleApplication18\ConsoleApplication18\Program.
cs:行号 21
请按任意键继续. . .
------解决思路----------------------
结果发错了:
arr.Length: 5
list2: 0
未经处理的异常: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值
并小于集合大小。
参数名: index
在 System.ThrowHelper.ThrowArgumentOutOfRangeException()
在 System.Collections.Generic.List`1.set_Item(Int32 index, T value)
在 ConsoleApplication18.Program.Main(String[] args) 位置 d:\用户目录\Document
s\Visual Studio 2012\Projects\ConsoleApplication18\ConsoleApplication18\Program.
cs:行号 21
请按任意键继续. . .
------解决思路----------------------
该实例为空,你只是指定了初始容量,但是集合里面什么都木有,贸然的给他list[i]当然会索引异常。
------解决思路----------------------
你不要假定相关数据是正常的
而是要验证他们是正常的后,方可计算
不然要异常处理做什么?