foreach

兔博士 2010-10-12 08:14:11
如何用 foreach 语句输出 这个数组:int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4,5,6} };
...全文
191 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
bmwdbm 2011-07-27
  • 打赏
  • 举报
回复
重拾老师原来讲过的
tx066415 2010-10-13
  • 打赏
  • 举报
回复
foreach (int[] arr in a)
{
foreach(int n in arr)
{
//这里是数组 至于最小数 加个变量 在里面循环比较吧
}
}
兔博士 2010-10-13
  • 打赏
  • 举报
回复
因为最近老是搞不懂foreach的用法
kensouterry 2010-10-13
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
class Program
{
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

public Program()
{
//依次输出数组中的所有数据
foreach (int temp in a)
{
Console.Write(temp + ",");
}

}

static void Main(string[] args)
{
Program p1 = new Program();

Console.ReadLine();
}
}
}


楼主为什么要使用foreach来获取最小值呢?
lyl1599 2010-10-13
  • 打赏
  • 举报
回复
学习了
火柴没帽 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyazhe 的回复:]

C# code
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
foreach (int item in a)
{
Console.WriteLine(item);
}
[/Quote]
+1
求最小值
static void Main(string[] args)
{
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
int min = a[0, 0];
foreach (int e in a)
{
if (e < min)
min = e;
}

Console.WriteLine(min);

Console.ReadLine();
}
pthiiu 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 flyerwing 的回复:]

引用 2 楼 huanshayi 的回复:
foreach (int[] arr in a)
{
foreach(int n in arr)
{
//这里写输出n的代码
}
}

估计可以吧
[/Quote]

我在vs2010里试过了,不行, 外层的foreach不能通过, 不能用 foreach(int[] arr in a)
flyerwing 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 huanshayi 的回复:]
foreach (int[] arr in a)
{
foreach(int n in arr)
{
//这里写输出n的代码
}
}
[/Quote]
估计可以吧
pthiiu 2010-10-13
  • 打赏
  • 举报
回复

对某种类型的元素的集合中的每个元素进行操作的时候用foreach,在过程中是调用的集合Enumerator来遍历这个集合中的每个元素,所以在遍历过程中一些会影响Enumerator的操作不能被执行,否则就会抛出异常(InvalidOperationException)


foreach(element_type element in element_collection)
{
//每个元素的值可以被更改,但是影响这个集合遍历的操作不能被执行,例如删除某个元素
do_something( element );
}


在最近的C#的版本中(C#3.0开始??VS2008),也可以用 var 来表示任意类型的元素

foreach(var element in element_collection)
....


希望能解释明白,我刚学的时候也是搞不清楚,只是用

for(int i = 0;i<lenght;i++)
....

这样的循环
nigerenz 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 z405158579 的回复:]
要是直接输出可以这样:

int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
foreach(int i in a)
{
Console.Write(i);
}

要想输出的效果是2行3列就这样:
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
forea……
[/Quote]

hujinn 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 pthiiu 的回复:]

C# code

int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

public int GetMinValue(int[,] a)
{
int minValue = int.MaxValue;
foreach (int n in a)
{

if(n<minValue) minValue = ……
[/Quote]
+1
pthiiu 2010-10-13
  • 打赏
  • 举报
回复

int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

public int GetMinValue(int[,] a)
{
int minValue = int.MaxValue;
foreach (int n in a)
{
if(n<minValue) minValue = n;
}
return minValue;
}
兔博士 2010-10-12
  • 打赏
  • 举报
回复
也是用foreach语句,怎样求出这个数组的最小值呢?
兔子-顾问 2010-10-12
  • 打赏
  • 举报
回复
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
foreach (int item in a)
{
Console.WriteLine(item);
}
z405158579 2010-10-12
  • 打赏
  • 举报
回复
要是直接输出可以这样:

int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
foreach(int i in a)
{
Console.Write(i);
}

要想输出的效果是2行3列就这样:
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
foreach(int i in a)
{
if(i==3)
{
Console.Write(i);
Console.Write("\n");
}
else
{
Console.Write(i);
}

}
huanshayi 2010-10-12
  • 打赏
  • 举报
回复
foreach (int[] arr in a)
{
foreach(int n in arr)
{
//这里写输出n的代码
}
}
wuyq11 2010-10-12
  • 打赏
  • 举报
回复
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{

}
}

111,129

社区成员

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

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

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