c#遍历二维List出错

[复制链接]
查看11 | 回复2 | 2009-3-10 13:06:12 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.Text;
namespace Cast
{

class Program

{

static void Main(string[] args)

{

List listTemp = new List();

List D2List = new List();

for (int i = 0; i < 10; i++)

{

for (int j = 0; j < 10; j++)

{

D2List.Add(i * j);

}

}

for (int m = 0; m < 9; m++)

{

for (int n = 0; n < 9; n++)

{

Console.Write(D2List[m][n].ToString());

}

}

System.Threading.Thread.Sleep(1000);

}

}
}
代码可直接复制
出错为Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

回复

使用道具 举报

千问 | 2009-3-10 13:06:12 | 显示全部楼层
你认真学习下实例,然后看下listTemp.Clear(); 你都把listTemp清空了,之前添加的也都清空了,所以根本就没有D2List里面根本就没有东西,把List listTemp = new List(); 写到for循环的第一个括号里面就好了...
回复

使用道具 举报

千问 | 2009-3-10 13:06:12 | 显示全部楼层
为什么不这么定义呢:List[] listTemp = new List(10);...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行