一:数组反转
模式一: 从小到大直接反转
int[] nums = new int[] { 23, 234, 3425, 1 };
Array.Sort(nums);
Array.Reverse(nums);
for (int i = 0; i < nums.Length; i++)
{
Console.Write(nums[i] + "\t");
}
Console.ReadKey();
模式二:从大到小排序
int[] nums = new int[] { 23, 234, 3425, 1 };
Array.Sort(nums);
for (int i = nums.Length - 1;i >= 0; i --)
{
Console.Write(nums[i] + "\t");
}
Console.ReadKey();
二: 调用数组(sumarry)
static void Main(string[] args)
{
Program.sumarray();
Console.ReadKey();
}
public static void sumarray()
{
int[] numbers = new int[] { 1, 2, 3, 5, 4 };
int sum=0;
for (int i=0;i<numbers.Length ;i++)
{
sum+=numbers[i];
}
Console.WriteLine("和为{0}", sum);
}
举例:求和
static void Main(string[] args)
{
int[] numbers = new int[] { 1, 2, 3, 5, 4 };
Program.sumarray(numbers);
Console.ReadKey();
}
public static void sumarray(int []nums)
{
int sum=0;
for (int i=0;i<nums.Length ;i++)
{
sum+=nums [i];
}
Console.WriteLine("和为{0}", sum);
}
三:字符串数组
static void Main(string[] args)
{
string [] names = new string [] { "老白","老总","Henry","老民" };
Program.addname(names);
Console.ReadKey();
}
public static void addname(string[] names)
{
string str = "";
for (int i = 0; i < names.Length - 1; i++)
{
str += names[i] + "|";
}
}
四:可变数组
public static void Show(string str,params int []strs)
{
}
static void Main(string[] args)
{
Console.WriteLine ("{0}{1}{3}",1,2,3);
Show("{0}{1}{2}{3}{4}{5}", 1, 2, 3, 4, 5, 6);
}
五:数组举例
1)将正整数转变为负数算法
int[] nums = new int[] { 0, -23, 34, -56, 78 };
for (int i = 0; i < nums.Length ; i++)
{
if (nums[i]>0)
{
nums[i]++;
}
if (nums[i]<0)
{
nums[i]--;
}
}
for (int i = 0; i < nums.Length ; i++)
{
Console.Write(nums[i] + "\t");
}
Console.ReadKey();
2)将字符串顺序反转
string[] text = { "我", "是", "好人" };
for (int i = text.Length - 1; i >= 0; i--)
{
Console.Write(text[i] + "\t");
}
Console.ReadKey();
||
string[] text = { "我", "是", "好人" };
string temp="";
for (int i = 0; i < text.Length /2; i++)
{
temp = text[i];
text[i] = text[text.Length - i - 1];
text[text.Length - 1 - i] = temp;
}
for (int i = 0; i < text.Length ; i++)
{
Console.ReadKey();
}