002-Several basic operations of an array

Source: Internet
Author: User

1  Packageoo2_array_code_processing;2 3 Importjava.util.Arrays;4 5 /**6  * @authorAlazy_cat7 * Typical array code processing:8 * 1. Find the largest element in the array9 * 2. Calculate the average of an array elementTen * 3. Copying an array One * 4. Reverse the order of the array elements A * 5. Multiply matrix -  */ -  Public classArraycodeprocessin { the      Public Static voidMain (string[] args) { -         int[] A =New int[8]; -          for(inti=0; i<a.length; i++) { -A[i] = (int) (Math.random () * 100); +         } -          +         int[] B = {{1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8, 9, 10}}; A         int[] C = {{1, 4}, {2, 5}, {3, 6}, {4, 7}}; atSystem.out.println ("Array:" +arrays.tostring (a)); -System.out.println ("The largest number in the array:" +Getmax (a)); -System.out.println ("The average value in the array:" +Getaverage (a)); -System.out.println ("Copy array:" +arrays.tostring (Copyarray (a))); -SYSTEM.OUT.PRINTLN ("Reverse array order:" +arrays.tostring (Reversearray (a))); -System.out.println ("Matrix multiplication:"); in         //the condition for matrix A to be multiplied by Matrix B is the number of columns of a = = number of rows of B -         if(B[0].length = =c.length) { to             int[] D =Matrixmultiply (b, c); +              for(inti=0; i<d.length; i++) { -                  for(intj=0; j<d[0].length; J + +) { theSystem.out.print (D[i][j] + ""); *                 } $ System.out.println ();Panax Notoginseng             } -}Else { the             Throw NewIllegalArgumentException ("These two matrices cannot be multiplied ..."); +         } A     } the     //find the largest element in an array +      Public Static intGetmax (int[] a) { -         intMax =Integer.min_value; $          for(intx:a) { $             if(X >max) -Max =x; -         } the         returnMax; -     }Wuyi     //computes the average of an array element the      Public Static DoubleGetaverage (int[] a) { -         intsum = 0; Wu         DoubleAverage = 0.0; -          for(intx:a) { AboutSum + =x; $         } -Average = sum/a.length; -         returnaverage; -     } A     //Copying an array +      Public Static int[] Copyarray (int[] a) { the         int[] B =New int[a.length]; -          for(inti=0; i<a.length; i++) { $B[i] =A[i]; the         } the         returnb; the     } the     //Reverse array element order -      Public Static int[] Reversearray (int[] a) { in         intMID = A.length/2; the         inttemp = 0; the          for(inti=0; i<mid; i++) { Abouttemp =A[i]; theA[i] = a[a.length-i-1]; theA[A.LENGTH-I-1] =temp; the         } +         returnA; -     } the     //matrix multiplicationBayi      Public Static int[] Matrixmultiply (int[] A,int[] b) { the         int[] C =New int[A.length] [B[0].length]; the          for(intI=r; k<a.length; k++) { -              for(inti=0; i<b[0].length; i++) { -                  for(intj=0; j<b.length; J + +) theC[k][i] + = a[k][j] *B[j][i]; the             } the         } the         returnC; -     } the}

002-Several basic operations 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.