foreach 循环遍历 以及函数的应用
foreach( 对集合每个元素的引用 in 集合 )
{
}
举例:
int[] a = new int[5]{1,2,3,4,5};
foreach( int b in a )
{
//b就是a中的每个元素
}
注意:
1.foreach只能对集合进行遍历。
2.foreach在操作集合的时候,只能读不能改;
3.foreach操作Dictionary<T,T>
Dictionary<string,string> dic = new Dictionary<string,string>();
//...添加值
foreach( KeyValuePare<string,string> b in dic)
{
}
4.foreach操作List<T>
List<int> list = new List<int>();
//添加值
foreach( int b in list )
{
}
5.foreach能够操作IEnumarable(IEnumarator)下的子级集合。
函数:
主要的功能就是为了让代码结构更加良好。
函数是——实现相对独立功能的程序代码模块(程序段).
函数的四要素:函数名,输入,输出,运算
有的函数没有输入,函数名后的小括号中可以不写东西,但必须要有小括号。
有的函数没有返回,在函数名的左侧不要写数据类型了,写void即可.
定义语法:
static 返回类型 函数名(形参列表)
{
函数体,运算
}
static int Max(int a,int b)
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
调用语法:
数据类型 变量名 = 函数名(实参列表);
int n = Max(7,8);
形参与实参一定要一一对应(个数,类型)
传值
把实参的数据做个复本,送到函数的形参中去。
一般基本类型(int,double,char,bool,DateTime)都是值类型,他们默认情况下会传值。
举例:
static void Main(string[] args)
{
int a = ;
Console.WriteLine(a);//??? 10
Add(a);
Console.WriteLine(a);//??? 10--实参的值传到调用的Add函数里,实参值并不改变,int型默认传值
}
static void Add(int x)
{
Console.WriteLine(x);//??? 10
x++;
Console.WriteLine(x);//??? 11
}
运行结果:

传址
把实参的地址传组函数的形参中去。形参与实参共用同一个数据空间。
一般引用类型都是传地址的,如数组和字符串。
举例:
static void Main(string[] args)
{
int[] a = new int[] {,, };
//显示
Show(a); //3 4 5
Add(a);
//显示 //数组默认的是传址,就是将地址传到所调用的函数里面,实参的值也会跟着改变
Show(a); //103 104 105
}
static void Show(int[] x)
{
foreach (int b in x)
{
Console.Write(b+"\t");
}
Console.WriteLine();
}
static void Add(int[] x)
{
//显示
Show(x); //3 4 5
for (int i = ; i < x.Length; i++)
{
x[i] = x[i] + ;
}
//显示 //103 104 105
Show(x);
}
运行结果:

练习题:
1.调用函数:青歌赛打分:20位评委给一个选手打分,去掉一个最高分,去掉一个最低分求歌手的平均得分。
static void Main1(string[] args)
{
//调用函数:青歌赛打分:20位评委给一个选手打分,去掉一个最高分,去掉一个最低分求歌手的平均得分。 //定义数组接收评委的分数
int[] fenshu=new int[];
int max = , min = ,sum=; //打分
fenshu = Shuru(fenshu); //运算
max = Max(fenshu);
min = Min(fenshu);
sum = Sum(fenshu);
//输出
Shuchu(fenshu,max,min,sum); }
static int [] Shuru(int[]a)//#######输入函数
{
for (int i = ; i <a.Length; i++)
{
Console.Write("请第{0}个评委打分:",i+);
a[i] = Convert.ToInt32(Console.ReadLine());
}
return a;
}
static int Max(int[] a)//#######求最大值函数
{
int zuida = ;
foreach (int b in a)
{
if (b>zuida)
{
zuida = b;
}
}
return zuida;
}
static int Min(int[] a)//#######求最小值函数
{
int zuixiao = ;
foreach (int b in a)
{
if (b<zuixiao)
{
zuixiao = b;
}
}
return zuixiao;
}
static int Sum(int[] a)//#######求和函数
{
int sum = ;
for (int i = ; i <a.Length; i++)
{
sum = sum + a[i];
}
return sum;
}
static void Shuchu(int[] a,int max,int min,int sum)//######输出函数
{
double avg =1.0* (sum - max - min) / (a.Length - );
Console.WriteLine("去掉一个最高分{0},去掉一个最低分{1},该选手的平均得分是:{2}",max,min,avg);
}
2.顺序查找法:输入一个数,看是否能在已知数组中找到,输出找到或没找到
static void Main2(string[] args)
{
//*******顺序查找法******* int[] a=new int[]{,,,,,,,};
//输入
int n = Convert.ToInt32(Console.ReadLine());
//查找
bool find = found(a,n); //输出
if (find==true)
{
Console.WriteLine("找到了");
}
else
{
Console.WriteLine("没找到");
}
} static bool found(int[] a, int n)
{
bool found = false;
foreach (int b in a)
{
if (b==n)
{
found = true;
break;
}
}
return found;
}
3.二分法查找,题目同上
static void Main3(string[] args)
{
//*******二分法查找******
int[] a = new int[] { , , , , , , , };
//输入
int n = Convert.ToInt32(Console.ReadLine()); //排序
a = Shunxu(a);
//查找(二分法)
bool find = zhaodao(a,n); //输出
if (find==true)
{
Console.WriteLine("找到了");
}
else
{
Console.WriteLine("没找到");
} }
static int[] Shunxu(int[] a)//排序函数,从小到大排序
{
for (int i = ; i <= a.Length - ; i++)
{
for (int j = ; j <= a.Length - i; j++)
{
if (a[j] < a[j - ])
{
int z = a[j];
a[j] = a[j - ];
a[j - ] = z;
}
}
}
return a;
}
static bool zhaodao(int[] a, int n)//查找函数
{
bool zhaodaole = false;
int ks = , js = a.Length - , zj;
for (; ; )
{ zj = (ks + js) / ;
if (a[zj]==n)
{ zhaodaole = true;
break; }
else
{
if (a[zj]>n)
{
js = zj - ;
}
else
{
ks = zj + ;
}
if (ks<js)
{
break;
}
} }
return zhaodaole;
}
foreach 循环遍历 以及函数的应用的更多相关文章
- yii2通过foreach循环遍历在一个用户组中取出id去另一表里查寻信息并且带着信息合并原数组信息---案例
yii2通过foreach循环遍历在一个用户组中取出id去另一表里查寻信息并且带着信息合并元数组信息---案例 public function actionRandomLists(){ //查询到了所 ...
- for循环和foreach循环遍历集合的效率比较
先上代码 package com.test; import java.util.ArrayList; import java.util.LinkedList; import java.util.Lis ...
- C# foreach 循环遍历数组
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- IT兄弟连 Java语法教程 数组 使用foreach循环遍历数组元素
从JDK5之后,Java提供了一种更简单的循环:foreach循环,也叫作增强for循环,这种循环遍历数组和集合更加简洁.使用foreach循环遍历数组和集合元素时,无需获得数组或集合的长度,无需根据 ...
- es6的foreach循环遍历
forEach forEach是Array新方法中最基本的一个,就是遍历,循环.例如下面这个例子: 结果: 这段代码相当于: for (var k = 0, length = array.length ...
- 二维数组遍历的方式(for普通循环遍历、foreach循环遍历、toString方式遍历)
package com.Summer_0421.cn; import java.lang.reflect.Array; import java.util.Arrays; /** * @author S ...
- php中foreach循环遍历二维数组
最近在用tp3.2框架,在查询的时候用到了select(),这条语句返回的是二维数组,所以在对返回的数据做处理时,遇到了些麻烦,百度了下foreach,终于用foreach解决了数据的筛选问题 (因为 ...
- mybatis问题。foreach循环遍历数组报错情况,及其解决方法
根据条件查询数据列表,mybatis查询代码如下 如果只查询属于特定部门拥有的数据权限.这需要用 String[ ] codes保存当前部门及其子部门的部门编码. 所以需要在mybatis中遍历编码数 ...
- 集合-新特性foreach循环遍历集合或项目
1.增强for循环对集合的遍历 点击查看代码 @Test //集合遍历 public void test3(){ Collection coll = new ArrayList(); coll.add ...
随机推荐
- react input 获取/失去焦点
<div className={ this.state.focus ? "dis_bottom_left_onfocus" : "dis_bottom_left&q ...
- Leetcode: 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- 在Unity中如何取得一个Box的Bounds
private BoxCollider mCollider; // Use this for initialization void Start () { mCollider = GetCompone ...
- jquery隐藏table表格的某一列
jquery隐藏table表格的某一列: $('table tr').find('td:eq(13)').hide(); 隐藏table的第13列
- 如何使用Android Studio开发/调试Android源码
本文是以源码中development/tools/idegen/README作为指导文档. 环境: Ubuntu 14.10,openJdk 1.7,Android Studio 1.0.2,andr ...
- DOM解析
1.xml可扩展标记语言(Extensible Makeup Language) 最简单的声明语法: <?xml version="1.0" ?> 用encoding属 ...
- Maps for Developers
苹果官方文档: Give your apps a sense of place with maps and location information. Present maps with custom ...
- win api 实现 AES加密、解密
WCHAR szPasswod[] = {L"goodpasswod" }; HCRYPTHASH hHash; HCRYPTPROV hCryptProv; HCRYPTKEY ...
- MVC系列1-MVC基础
终于决定写一个系列的文章了,最开始其实是准备写一下WPF的,因为我这两年一直在做WPF,对WPF的喜爱自然是无以言表.但是由于我所在的地区对WPF的普及不是很广泛,所以,被迫又开始做起来web,但是我 ...
- Android安全研究经验谈
安全研究做什么 从攻击角度举例,可以是:对某个模块进行漏洞挖掘的方法,对某个漏洞进行利用的技术,通过逆向工程破解程序.解密数据,对系统或应用进行感染.劫持等破坏安全性的攻击技术等. 而防御上则是:查杀 ...