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