Defines an array that gets the maximum and minimum values in the array

Source: Internet
Author: User

Ideas:


We define an array and then define a max variable to store the maximum value;

Then define a min variable to store the minimum value;


We iterate through the array, and if the current value is greater than Max, the current value is assigned to Max;

If the current value is less than min, the current value assigned to min;


Let's give the sample code:

1 2 3 4 5 6 7 8 9 A-public  class  test {       public  static  void  Main (String[] args)  {          int  arr[]= new  int []{1, 6, 7,-2}; //  set Semantic an array          int  max=arr[0]; //  The 1th element in the data save Max           int  min=arr[0]; //  The 1th element in the data save min           for (int  i= 1; i<arr.length;i++) { //  traverses the array starting with the second element               if (arr[i]>max) {  

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.