体验C#——使用数组

11 篇文章 0 订阅
11 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayTest
{
    class One_dimensional_array
    {
        public void test()
        {
            string[] name1;
            //动态初始化
            name1 = new string[3] { "C", "C++", "java" };
            //静态初始化
            string[] name2 = { "java thinking", "Win32ASM", "android" };
            //使用下标
            Console.WriteLine(name1[1]);
            Console.WriteLine(name2[2]);
            //数组元素的个数(length)
            Console.WriteLine("name1数组的长度为{0}", name1.Length);
            Console.WriteLine("name2数组的长度为{0}", name2.Length);
            //用for语句输出name1的所有元素
            for (int i = 0; i < name1.Length; i++)
            {
                Console.WriteLine("name1书名:{0}", name1[i]);
            }
            //用foreach输出name2的所有元素
            foreach (string str in name2)
            {
                Console.WriteLine("name2书名:{0}", str);
            }
        }
    }
    class Two_dimensional_array
    {
        public void test() 
        {
            //动态初始化
            string[,] book;
            book = new string[2,2]{{"IOS","计算机"},{"平凡的世界","论语"}};
            //静态初始化
            //string[2][2] book2= {{"文学","计算机"},{"平凡的世界","Android sec"}};
            string[,] book2={{"文学","计算机"},{"平凡的世界","Android sec"}};
            //使用下标
            Console.WriteLine(book[1,1]);
            Console.WriteLine(book2[2,2]);
            //数组元素的个数(length)
            Console.WriteLine("name1数组的长度为{0}", book.Length);
            Console.WriteLine("name2数组的长度为{0}", book2.Length);
            //用for语句输出name1的所有元素
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2;j++ )
                    Console.WriteLine("专业书名:{0}", book[i,j]);
            }
            //用foreach输出name2的所有元素
            foreach (string str1 in book2)
            {
                Console.WriteLine("book2书名:{0}", str);
            }
        }
    }
    class Program
    {
        /*
         * 用数组来存储书名;
         * */
        static void Main(string[] args)
        {
            Two_dimensional_array two = new Two_dimensional_array();
            two.test();
           Console.ReadKey();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值