怎样给二维数组的一整行来赋值呢?

dog_huang 2012-03-11 02:48:32
比方说
private int[,] position = new int[12, 3];
position[0]={2,5,1};
为什么不行?应该怎样去写呢?我不想36个数字一个一个写啊。。。
...全文
3746 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dog_huang 2012-03-12
  • 打赏
  • 举报
回复
感觉不错,我回头试试去~[Quote=引用 6 楼 crystal_lz 的回复:]
int[][] test = new int[12][];
test[0] = new int[]{ 1, 2, 3 };
MessageBox.Show(test[0][0] + " " + test[0][1] + " " + test[0][2]);
这是我的想法、、、锯齿数组、、
[/Quote]
crystal_lz 2012-03-11
  • 打赏
  • 举报
回复
int[][] test = new int[12][];
test[0] = new int[]{ 1, 2, 3 };
MessageBox.Show(test[0][0] + " " + test[0][1] + " " + test[0][2]);
这是我的想法、、、锯齿数组、、
helloysn 2012-03-11
  • 打赏
  • 举报
回复
你可以 int[] itemp=new int[]{2,5,1,*****};
int k=0;
for(int i=0;i<12;i++)
for(int j=0;j<3;j++)
{
position[i][j]=itemp[k++];
}
全国话费充值 2012-03-11
  • 打赏
  • 举报
回复
int[,] position = new int[,]{{1,2},{2,3},{4,5}};
  • 打赏
  • 举报
回复
[Quote=引用楼主 dog_huang 的回复:]
比方说
private int[,] position = new int[12, 3];
position[0]={2,5,1};
为什么不行?应该怎样去写呢?我不想36个数字一个一个写啊。。。
[/Quote]

你可以自己先实现一个通用的方法例如
public void SetupDimensionalArray<T>(T[,] array, T[]  values, int lineNum)
{
....
}
dog_huang 2012-03-11
  • 打赏
  • 举报
回复
那不在初始化的时候呢?有整行写的方法吗?[Quote=引用 1 楼 bdmh 的回复:]

position[0]={2,5,1};
这种写法只能用在初始化时,比如 int[] a = new int[] { 1, 2, 3 };
[/Quote]
bdmh 2012-03-11
  • 打赏
  • 举报
回复
position[0]={2,5,1};
这种写法只能用在初始化时,比如 int[] a = new int[] { 1, 2, 3 };

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧