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();
}
}
}
体验C#——使用数组
最新推荐文章于 2023-10-27 16:35:24 发布