class to find the maximum minimum average of an array

Source: Internet
Author: User

Package com.ctu.java.exer;public class testarray {public static void main ( String[] args)  {int[] arr = new int[]{12,4,76,0,-98,-54,4,100}; Arrayutil au = new arrayutil (); Int max = au.getmax (arr); SYSTEM.OUT.PRINTLN ("Maximum value:"  + max); Int avg = au.avg (arr); System.out.println ("Average value:"  + avg); Au.printarray (arr); System.out.println ("Inverted array:"  ); Au.reverse (arr); Au.printarray (arr); System.out.println ("Sort by array:"); Au.sort (arr, "ASC"); Au.printarray (arr);// todo auto-generated  method stub}package com.ctu.java.exer;public class arrayutil {public int  Getmax (Int[] arr) {int max = arr[0];for (int i = 1; i <  arr.length;i++) {if (Max < arr[i]) {max = arr[i];}} Return max;} Public int getmin (Int[] arr) {int min  = arr[0];for (int i = 1; i < arr.length;i++) {if (Min > arr[i]) {min =  Arr[i];}} return min ;} Public void printarray (Int[] arr) {System.out.print ("["); for (int i = 0; i  < arr.length;i++) {System.out.print (arr[i]+  "\ t");} System.out.println ("]");} Public int avg (Int[] arr) {int sum = getsum (arr); return sum/arr.length;} Public int getsum (Int[] arr) {int sum = 0;for (int i =0 ; i  < arr.length;i++) {sum +=arr[i];} Return sum;} Public int[] reverse (Int[] arr) {for (int x = 0,y = arr.length -1;  x<y;x++,y--) {int temp = arr[x];arr[x] = arr[y];arr[y] = temp;} return arr ;} Public int[] copy (Int[] arr) {int[] arr1 = new int [arr.length];for (int  i = 0; i < arr.length ;i++) {arr1[i] = arr[i];} RETURN&NBSP;ARR1;} Public void sort (INT[]&NBSP;ARR,STRING&NBSP;DESC) {if (desc ==  "ASC") {for (int i =  0; i < arr.length-1;i++) {for (int j = 0;j < arr.length  - 1 -i;j++) {     if (arr[j] > arr[j + 1]) {swap ( arr,j,j+1);}}} Else if (desc ==  "desc") {for (int i = 0; i < arr.length-1;i++) { for (int j = 0;j < arr.length - 1 -i;j++) {      if (Arr[j] < arr[j + 1]) {     swap (arr,j,j+1);}}} ELSE{SYSTEM.OUT.PRINTLN ("You entered the wrong way!") ");}} Public void swap (int[] arr,int i ,int j) {Int temp = arr[i];arr[i]  = arr[j];arr[j] = temp;}


}


class to find the maximum minimum average of an array

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.