. NET, have you forgotten? (iii)--about the use of array and list

Source: Internet
Author: User
Tags shuffle

Before, had been talking. NET Framework aspects of the problem, today to talk about the use of array and list, this should be considered the most basic of the algorithm of things. Just to remind you to pay attention to this problem.

Write this because of a classmate's help, the thing is like this, he is responsible for the company's a training module, in the training module, there is a function of automatic volume. Then, it's easy to think of the shuffle algorithm. So I explained to him the process and steps of the shuffle algorithm, and he gave me the code and proudly told me that he used generics ...

List<int> list = new List<int>();
for (int i = 0; i < 10; i++)
{
   list.Add(i);
}
Random r = new Random();
for (int j = 0; j < 100; j++)
{
   int temp;
   int x1 = r.Next(10);
   int x2 = r.Next(10);
   temp = list[x1];
   list[x1] = list[x2];
   list[x2] = temp;
}

I gently told him that he had a bad method and wrote down the following code:

int[] array = new int[10];
for (int i = 0; i < 10; i++)
{
   array[i] = i;
}
Random r = new Random();
for (int j = 0; j < 100; j++)
{
   int temp;
   int x1 = r.Next(10);
   int x2 = r.Next(10);
   temp = array[x1];
   array[x1] = array[x2];
   array[x2] = temp;
}

He said to me very disdain, not all the same! And still in the use of generics for the proud! I have no words ...

I just replaced the list with an array, and some people would say, is that a big relationship?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.