学习c#第十二天(数组之元素遍历)

本文详细介绍了C#中for循环和foreach遍历数组的方法,展示了如何遍历整型、浮点型、双精度型数组,并提及了数组元素的默认初始值。
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace ForEachStruct
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //1.for循环遍历数组
            /*
            int[] intArray;
            intArray = new int[5];
            intArray[0] = 10;
            intArray[1] = 12;
            intArray[2] = 14;

            Console.WriteLine(intArray[1]);
            Console.ReadKey();
            */
            /*
            int[] intArray = new int[] { 11, 22, 33, 44, 55, 66, 77, 88, 99 };

            //遍历数组.
            //intArray.length:可以取得当前数组的长度.9.
            //Console.WriteLine(intArray.Length);
            for(int i =0;i <= intArray.Length - 1; i++)
            {
                Console.WriteLine(intArray[i]);
            }
            Console.ReadKey();
            */
            /*
            string[] str = new string[5]; //数组声明与初始化
            //数组的元素赋值.
            for(int i =0; i < str.Length; i++)
            {
                str[i] = "元素" + i;
            }
            //循环取值.
            for(int j = 0; j < str.Length; j++)
            {
                Console.WriteLine(str[j]);
            }
            Console.ReadKey();
            */

            //例:定义一个字符串数组,存放12生肖,循环遍历这个数组,将元素
            //组合成一个字符串,且元素与元素之间用'|'分割。
            //12生肖数组元素如下:"子鼠","丑牛","寅虎","巳蛇","午马","未羊","
            //"申猴","酉鸡","戌狗","亥猪"
            /*
            string[] animals = new string[] {"子鼠","丑牛","寅虎","巳蛇","午马","未羊","申猴","酉鸡","戌狗","亥猪"};
            String animalStr = "";

            for (int i = 0; i < animals.Length; i++)
            {
                animalStr += animals[i];
                if (i <animals.Length - 1)
                {
                    animalStr += "|";
                }
            }

            Console.WriteLine(animalStr);
            Console.ReadKey();
            */

            //2.foreach遍历数组
            //语法
            //foreach(数组数据类型 临时变量 in 数组名)
            //{
            //    Console.WriteLine(临时变量)
            //}
            /*
            int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9};
            foreach (int i in intArray)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
            */

            //3.数组元素的初始值
            //数据类型[] 数组名 = new 数据类型[数组长度];
            //数组的声明,初始化.五个元素的存储空间,已经在内存中开辟了.

            int[] intArray = new int[5];
            float[] fArray = new float[5];
            double[] dArrary = new double[5];
            string[] sArrary = new string[5];
            bool[] bArrary = new bool[5];

            //数组初始化完成后,各种类型的数组的默认值
            //int[], 元素的值默认都是0;
            //float[],元素的值默认都是0;
            //double[], 元素的值默认都是0.0;
            //string[],元素的值默认都是null;
            //bool[], 元素的值默认都是false;

            //遍历 int[]
            foreach (int i in intArray)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine("------------");

            //遍历 float[]
            foreach (int f in fArray)
            {
                Console.WriteLine(f) ;
            }

            Console.WriteLine("------------");

            //遍历 double[]
            foreach (int d in dArrary)
            {
                Console.WriteLine(d);
            }

            Console.WriteLine("------------");

            //遍历 string[]
            foreach (string s in sArrary)
            {
                Console.WriteLine(s);
            }
        
            Console.WriteLine("------------");

            //遍历 bool[]
            foreach (bool b in bArrary)
            {
                Console.WriteLine(b);
            }
            Console.ReadKey();

        }
    }
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值