Array定义
int[] array = new int[5] { 0, 1, 2, 3, 4 };
int[] arr1 = new int[] { 0, 1, 2, 3, 4 };
int[] arr2 = { 0, 1, 2, 3, 4 };
Console.WriteLine("数组长度为:{0}", array.Length);
Console.WriteLine("数组长度为{0}", arr1.Length);
Console.WriteLine("数组长度为{0}", arr2.Length);
Array应用经典冒泡
class Program
{
static void Main(string[] args)
{
int[] bub = new int[5] {10,20,30,40,50};
int temp;
for (int i = 0; i < bub.Length - 1; i++)
{
for (int j = 0; j < bub.Length - 1 - i; j++)
{
if (bub[j] < bub[j + 1])
{
temp = bub[j];
bub[j] = bub[j + 1];
bub[j + 1] = temp;
}
}
}
for (int i = 0; i < bub.Length;i++ )
{
Console.WriteLine(bub[i]);
}
Console.ReadKey();
}
}
Array冒泡简单写法
int[] array = { 2, 9, 5, 6, 8 };
Array.Sort(array);
foreach (int i in array)
{
Console.WriteLine(i.ToString());
}
lambda应用经典冒泡
static void Main(string[] args)
{
int[] valus = { 12, -1, 32, 3, 5 };
//实现冒泡排序
foreach (int i in valus.OrderBy(i => i)) {
Console.WriteLine(i);
}
Console.ReadKey();
}
当然用Linq也可以写,方式很多,就不一一写出来了
Array 冒泡之字符串顺序
static void Main(string[] args)
{
string[] names = new string[] { "长","大","江","设" };
string temp;
for (int i = 0; i < names.Length/2; i++)
{
temp = names[i];
names[i] = names[names.Length - 1-i];
names[names.Length -1- i] = temp;
}
for (int i = 0; i < names.Length;i++ )
{
Console.WriteLine(names[i]);
}
//int age = ReadInt();
maobao();
Console.ReadKey();
}
Array 得奇偶
int[] array = { 4, 6, 7, 8, 9, 12, 13, 15 };
int total = 0;
int total1 = 0;
Console.WriteLine("偶数:");
for (int i = 0; i < array.Length; i++)
{
if (array[i] % 2 == 0)
{
total++;
Console.Write("{0},", array[i]);
continue;
}
}
Console.WriteLine("\n奇数:");
for (int i = 0; i < array.Length; i++)
{
if (array[i] % 2 != 0)
{
total1++;
Console.Write("{0},", array[i]);
continue;
}
}
Console.WriteLine("\n偶数总数为{0}", total);
Console.WriteLine("奇数总数为{0}", total1);