---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
关于c#数组的一些操作:
(1)颠倒数组
string[] s ={ "1","a","5","haha"};
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine("原始数组:{0}",s[i]);
}
Console.WriteLine("+++++++++++++++++++++++++");
string temp="";
for (int i = 0; i < s.Length / 2; i++)
{
temp = s[i];
s[i] = s[s.Length - i - 1];
s[s.Length - i - 1] = temp;
}
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine("颠倒的数组:{0}",s[i]);
}
(2)拼接数组
int[] nums1 ={ 1,2,4 };
int[] nums2 ={ 3,33,56566,63};
int[] nums3 = new int[nums1.Length + nums2.Length];
for (int i = 0; i < nums1.Length; i++)
{
Console.WriteLine("数组一:"+nums1[i]);
}
Console.WriteLine("=========================================");
for (int i = 0; i < nums2.Length; i++)
{
Console.WriteLine("数组二:"+nums2[i]);
}
Console.WriteLine("=========================================");
for (int i = 0; i < nums1.Length;i++ )
{
nums3[i] = nums1[i];
}
for (int i = nums1.Length, j = 0; i < nums3.Length; i++, j++)
{
nums3[i] = nums2[j];
}
//拼接操作的另一个思路://for (int i = 0; i < nums2.Length; i++)
//{
// nums3[nums1.Length + i] = nums2[i];
//}
for (int i = 0; i < nums3.Length; i++)
{
Console.WriteLine("拼接后的数组三:"+nums3[i]);
}
(3)从一个整数数组中取出最大的整数
int[] nums ={2,45,356,3,82 };
int temp = 0;
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] > temp)
{
temp = nums[i];
}
}
Console.WriteLine("nums数组中最大的整数是:{0}",temp);
(4)计算一个整数数组的所有元素的和
int[] nums ={ 1,2,3,4,5};
int temp = 0;
for (int i = 0; i < nums.Length; i++)
{
temp+=nums[i];
}
Console.WriteLine("nums数组的所有元素的和是:{0}",temp);
----------------------Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
详细请查看:http://net.itheima.com/