C # Object-oriented method of writing array functions

Source: Internet
Author: User

The previous article used Java methods to write out the functions that can be performed on an array, and then invoke these methods with the instantiated object to implement these functions.

This essay is used in the C # language to achieve the same functionality

Method class: Array

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceConsoleApplication18 {9     classArrayTen     { One         /// <summary> A         ///iterate through the array and output -         /// </summary> -         /// <param name= "arr" ></param> the         /// <returns></returns> -          Public int[] Showarr (int[] arr) { -              for(inti =0; I < arr. length;i++ ) -             { +Console.Write (arr[i]+"  "); -             } +             returnarr; A         } at  -         /// <summary> -         ///Create a method to copy an array arrcopy () -         /// </summary> -         /// <param name= "arr" ></param> -         /// <returns></returns> in          Public int[] Arrcopy (int[] arr) { -             int[] Fuzhi =New int[arr. Length]; to              for(intI=0; I<arr. length;i++){ +Fuzhi[i] =Arr[i]; -             } the             //after copying, iterate through the new array and output *Console.WriteLine ("the copied array is:"); $              for(inti =0; I < Fuzhi. length;i++ )Panax Notoginseng             { -Console.Write (fuzhi[i]+"  "); the             } +             returnFuzhi; A         } the         /// <summary> +         ///to set a method for implementing the inverse of an array Fanzhuan () -         /// </summary> $         /// <param name= "arr" ></param> $         /// <returns></returns> -          Public int[] Fanzhuan (int[] arr) { -              for(inti =0; I < arr. Length/2; i++ ) the             { -                 inttemp =0;Wuyitemp =Arr[i]; theArr[i] = Arr[arr. Length-1-i]; -Arr[arr. Length-1-I] =temp; Wu             } -             returnarr; About         } $         /// <summary> -         ///Bubble Sort Method sort () -         /// </summary> -         /// <param name= "arr" ></param> A          Public voidSortint[] arr) +         { the              for(inti =0; I < arr. length-1; i++)//run arr altogether. Length of the row in good order -             { $                  for(intj =0; J <arr. length-1-i;j++)//number of comparisons per trip the                 { the                    if(Arr[j] > arr[j+1]){ the                         inttemp =Arr[j]; theARR[J] = arr[j+1]; -arr[j+1] =temp; in                     } the                 } the             } About         } the     } the}

Main Method Class: Program

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceConsoleApplication18 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -             //create an array of arr -             int[] arr =New int[] {1,2,5, -6, at, -9, -, - }; the             //Initialize Array class -Array A1 =NewArray (); -A1.arrcopy (arr);//method to invoke copy array arrcopy () -  +Console.WriteLine ("\ n The inverted array is:"); -A1.fanzhuan (arr);//call inversion method to implement the inverse of an array +A1.showarr (arr);//iterate over an array after arr A  atConsole.WriteLine ("\ n The bubble sorted array is:"); -A1.sort (arr);//call method to sort arr - A1.showarr (arr); -  - Console.readkey (); -         } in     } -}

Operation Result:

C # Object-oriented method of writing array functions

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.