Public classArrayutil {//to find the maximum value of an array Public intGetmax (int[] arr) { intmax = Arr[0]; for(inti = 1; i < arr.length; i++) { if(Max <Arr[i]) {Max=Arr[i]; } } returnMax; } //to find the minimum value of an array Public intGetmin (int[] arr) { intMin = arr[0]; for(inti = 1; i < arr.length; i++) { if(Min >Arr[i]) {min=Arr[i]; } } returnmin; } //iterating over array elements Public voidPrintArray (int[] arr) {System.out.print ("["); for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+ "\ T"); } System.out.println ("]"); } //find the average of an array Public intAvgint[] arr) { intsum =getsum (arr); returnSum/arr.length; } //find the sum of the arrays Public intGetsum (int[] arr) { intsum = 0; for(inti = 0; i < arr.length; i++) {sum+=Arr[i]; } returnsum; } //inverse of an array Public int[] Reverse (int[] arr) { for(intx = 0, y = arr.length-1; x < y; x + +, y--) { inttemp =Arr[x]; ARR[X]=Arr[y]; Arr[y]=temp; } returnarr; } //implementing copying of arrays Public int[] Copy (int[] arr) { //int[] arr1 = arr; //return arr1; int[] arr1 =New int[Arr.length]; for(inti = 0; i < arr.length; i++) {Arr1[i]=Arr[i]; } returnarr1; } //sorting an array Public voidSortint[] arr, String desc) { if(desc = = "ASC") {//ascend: From small to large for(inti = 0; i < arr.length-1; i++) { for(intj = 0; J < arr.length-1-I; J + +) { if(Arr[j] > arr[j + 1]) {Swap (Arr,j,j+1); //Swap (arr[j],arr[j + 1]); } } } } Else if(desc = = "desc") {//from big to small for(inti = 0; i < arr.length-1; i++) { for(intj = 0; J < arr.length-1-I; J + +) { if(Arr[j] < Arr[j + 1]) {Swap (Arr,j,j+1); //Swap (arr[j],arr[j + 1]); } } } } Else{System.out.println ("You entered the wrong sort!" "); } } //swaps an element at a specified position in an array: swaps two elements indexed as I and J in the ARR array Public voidSwapint[] arr,intIintj) { inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }//Public void Swap (int i,int j) {//int temp = i;//i = j;//j = temp;// }}
The creation and use of Java learning--arrayutil