Dark Horse Programmer ———— Array Tool class object creation

Source: Internet
Author: User

today I learned the establishment of an array tool class object to consolidate knowledge of previous learning statements, functions, arrays, etc./**Create a tool class for manipulating arrays, including functions that are common to array operations such as: maximum, sort, etc.@authorlostnotes*/ Public classarrarytool{PrivateArraytool () {}//The methods in this class are static, so there is no need to create objects. //to ensure that other members do not create the class object, the constructor can be privatized. }/**gets the maximum value of an array of integer types@paramarr receives an array of elements of type int@paramthe largest element of the array. */ Public Static intGetmax (int[] arr) {    intMaxindex = 0;  for(intx = 0;x < arr.length;x++)    {        if(Arr[x] >Arr[maxindex]) Maxindex=x; }    returnArr[maxindex];}/**Select Sort by array@paramarr receives an array of elements of type int*/ Public Static voidSelectsort (int[] arr) {     for(intx = 0; x<arr.length-1;x++)    {         for(inty = x+1; y<arr.length;y++)        {                if(arr[x]>Arr[y]) SWP (arr,x,y); }    }}/**used to position displacement of elements in an array. @paramarr receives an array of elements of type int. @parama@paramb*/ Public Static voidSwap (int[] arr,intAintb) {    inttemp =Arr[a]; Arr[a]=Arr[b]; ARR[B]=temp;}/**gets the index of the specified element in the specified array@paramarr receives an array of type int. @paramkey to find the element@paramreturns the position of the first occurrence of the element if no return-1 is present*/ Public Static intGetIndex (int[] arr,intkey) {     for(intx = 0; x<arr.length;x++)    {            if(arr[x]==key)returnx; }    return-1;}/**converts an int array into a string. The format is: [E1,e2,e3 ...]@paramarr receives an array of elements of type int@paramreturns the string representation of the array*/ Public StaticString arraytosring (int[] arr) {String str= "[";  for(intx = 0;x<arr.length;x++)    {    if(x!=arr.length-1) Str= arr[x]+ ","; ElseStr= arr[x]+ "]"; }    returnstr;}

Dark Horse Programmer ———— Array Tool class object creation

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.