日期:2014-05-17  浏览次数:20791 次

获取Arraylist数组当中的数组元素的小问题
string[] info = new string[3];
ArrayList infolist = new ArrayList[];

info[0] = "a";
info[1] = "b";
info[2] = "c";

infolist.Add(info);

想要获取a
尝试过infolist[0][0],结果不行……应该怎么获取呢?

------解决方案--------------------
ArrayList中的每个元素是object类型的,需要转换成string[]才行:

写成(infolist[0] as string[])[0]


另外建议用泛型而不是ArrayList,即List<string[]> infolist=new ...
------解决方案--------------------
var info = new string[3];
var infolist = new ArrayList();

info[0] = "a";
info[1] = "b";
info[2] = "c";

infolist.Add(info);

var b = (string[])infolist[1];
var c = b[1];