class Program
{
static void Main(string[] args)
{
// 第一种声明方式 存放类型[] 数组名字
int[] arr = new int[] {1,2,3,4,5 };//数组放入元素;固定好类型不能放字符串或其他类型的元素;
int[] arr1 = new int[5] { 1, 2, 3, 4, 5 };//固定数组长度后面就不能改变;
int[] arr2 = new int[5];//重新new数组;改变长度;
arr2 = new int[3] ;
arr2[arr2.Length-1] = 22;//给数组下标数组赋值;最后一个元素;
for (int i = 0; i < arr.Length;i++ )
{
Console.WriteLine(arr[i]);
}
Console.WriteLine(arr2[arr2.Length - 1]);
int[] array = new int[] { };//数组整形初始化格式;
string[] Str = new string[] { };//数组字符串格式;
double[] Double = new double[] { };//数组小数点格式;
// 练 数组格式
int[] ArrayInt = new int[] { };
string[] arrStr = new string[] { };
double[] arrDou = new double[] { };
int[] arrInt = new int[]{ };
string[] arrName=new string[4]{"万","孙","黄","高"};
for(int i=0;i<arrName.Length;i++){
Console.WriteLine(arrName[i]);
}
//int[] arrInt = new int[] { };
//arrInt=new int[10];
//for (int i = 0; i < arrInt.Length;i++ )
//{
// arrInt[i] = i + 1;
// // Console.WriteLine(i+1);
//}
//foreach语句的应用;
foreach(int Int1 in arrInt){
Console.WriteLine(Int1);
}
//第二种声明方式 存放类型[]数组名字={1,2,3,4,5}直接赋值;
bool[] ArrBool = { false, true, 6 > 5 };
Console.WriteLine(ArrBool[0]);
Console.WriteLine(ArrBool[1]);
Console.WriteLine(ArrBool[2]);
Console.WriteLine("\n");
foreach (bool Bool in ArrBool)
{
Console.WriteLine(Bool);
}
Console.WriteLine("\n");
foreach (bool Bools in ArrBool)
{
if (Bools)
{
Console.WriteLine(Bools);
}
}
int[] ArrInt = new int[] { };
ArrInt = new int[50];
for (int i = 0; i < ArrInt.Length; i++)
{
ArrInt[i] = 2 * i + 1;
}
foreach (int Int in ArrInt)
{
Console.WriteLine(Int);
}
//练习
int[] arrInt = new int[] { };
int[] MyInt = new int[24];
int j = 0;
arrInt = new int[50];
for (int i = 0; i < arrInt.Length; i++)
{
arrInt[i] = 2 * i + 1;
if (arrInt[i] % 5 == 0 || arrInt[i] % 3 == 0)
{
MyInt[j] = arrInt[i];
j++;
}
}
Console.Write(j);
Console.WriteLine("\n这些数为:");
foreach (int arrInt1 in MyInt)
{
Console.Write(arrInt1 + " ");
}
Console.WriteLine("\n\n偶数为:");
int[] arr = new int[] { };
arr = new int[100];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i + 1;
if (arr[i] % 2 == 0)
{
Console.Write(arr[i] + " ");
}
}
Console.WriteLine("\n\n 小数为:");
double[] arrDou = new double[] { };
arrDou = new double[10];
for (int i = 0; i < arrDou.Length; i++)
{
arrDou[i] = i + 0.123;
}
foreach (double Dou in arrDou)
{
Console.WriteLine(Dou);
}
Console.WriteLine("100以内奇数被5或3整除");
int[] arrInt = new int[] { };
arrInt = new int[50];
int num = 0;
for (int i = 0; i < arrInt.Length; i++)
{
arrInt[i] = 2 * i + 1;
if (arrInt[i] % 3 == 0 || arrInt[i] % 5 == 0)
{
num++;
}
}
int[] arrInt1 = new int[num];
int j = 0;
for (int i = 0; i < arrInt.Length; i++)
{
if (arrInt[i] % 3 == 0 || arrInt[i] % 5 == 0)
{
arrInt1[j] = arrInt[i];
j++;
}
}
foreach (int arrInt2 in arrInt1)
{
Console.WriteLine(arrInt2);
}
Console.WriteLine("\n\n" + num);
Console.ReadKey();
}
}
unity游戏开发-C#语言基础篇(一维数组)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...