数组作为方法参数时的一些意外情况

数组作为方法参数时的一些意外情况

今天在刷题时,使用数组作为方法的参数,出现了一些意外情况.

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyArray
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             int[] nums = { 1, 2, 4, 5, 3, 2, 5, 6, 3 };
13             Console.WriteLine("Array numbers:");
14             foreach (int num in nums)
15                 Console.Write(num + "     ");
16 
17             Sort(nums);
18             Console.WriteLine("\nAfter Sort:");
19             foreach (int num in nums)
20                 Console.Write(num + "     ");
21 
22             Reverse(nums);
23             Console.WriteLine("\nAfter Reverse:");
24             foreach (int num in nums)
25                 Console.Write(num + "     ");
26 
27             Change(nums);
28             Console.WriteLine("\nAfter Change:");
29             foreach (int num in nums)
30                 Console.Write(num + "     ");
31 
32             SortAndChange(nums);
33             Console.WriteLine("\nAfter SortAndChange:");
34             foreach (int num in nums)
35                 Console.Write(num + "     ");
36 
37             DistinctAndChange(nums);
38             Console.WriteLine("\nAfter DistinctAndChange:");
39             foreach (int num in nums)
40                 Console.Write(num + "     ");
41 
42             Console.Read();
43         }
44 
45         static void Sort(int[] nums)
46         {
47             nums = (from l in nums orderby l select l).ToArray();
48         }
49 
50         static void Reverse(int[] nums)
51         {
52             nums = nums.Reverse().ToArray();
53         }
54 
55         static void Change(int[] nums)
56         {
57             for (int i = 0; i < nums.Length - 1; i++)
58                 nums[i] = nums[i + 1];
59         }
60 
61         static void SortAndChange(int[] nums)
62         {
63             nums = (from l in nums orderby l select l).ToArray();
64             for (int i = 0; i < nums.Length - 1; i++)
65                 nums[i] = nums[i + 1];
66         }
67 
68         static void DistinctAndChange(int[] nums)
69         {
70             int[] result = nums.Distinct().ToArray();
71             for (int i = 0; i < nums.Length - 1; i++)
72                 nums[i] = nums[i + 1];
73         }
74     }
75 }
View Code

结果:

 

posted @ 2016-08-27 14:34  Johar  阅读(250)  评论(0编辑  收藏  举报