Operation of the Java language Base array

Source: Internet
Author: User

Traversal of an array

 1 class  demo3_array{2 3 4 public static void GetElement (Int.  [] arr) {5 for (int i =0;i& lt;arr.length;i++ ) {6  System.out.println (Arr[i]); 7 } 8 } 9 Pub Lic static void  main (string[] args) {int[] arr = new int[]{11,22,33,44,55 };13 +  System.out.println ( ARR); System.out.println (arr[0 ]), System.out.println (arr[1 ]), System.out.println (arr[2 ]); System.out.println (arr[3 ]), System.out.println ("-----------------------------------" ); 21 22 23 The length property of the for (int i=0;i<arr.length;i++) {//Array object is  System.out.println (Arr[i]); }26 27 28 System.out.println ("-----------------------------------" ), int[] arr2 = {33,22 };31  GetElement (ARR2);}38 }           

To find the largest value in an array

1 class demo3_array{2  3  4 public     static void Getmax (int[] arr) {5         int. max = Arr[0]; 6             for (int i =0;i<arr.length;i++) {7                 if (max<arr[i]) {8                     max = }15) public static void Main (string[] args) {int[] arr2 = {22,33,55,11,88,9,5 }26}       

Array inversion

1 class demo3_array{2  3  4 public     static void Getmax (int[] arr) {5         int[] newArr = new int[arr. Length]; 6         int index = 0; 7  8 for             (int i =arr.length-1;i>0;i--) {9                 newarr[index]=arr[i];10 index++ }12 for (int i=0;i<newarr.length;i++ }17, public static void main (string[] args) {int[] arr2 = {22,33,55,11,88,9,5 getmax (arr2);         

To find the index number by value

1 class demo5{2 public     static void main (string[] args) {3         int[] arr = {11,22,33,44,55}; 4         int in Dex = GetIndex (arr,55        System.out.println (index); 6   } 8 9 public static int GetIndex (int[] Arr,int V Alue) {Ten for (int i=0;i<arr.length;i++) {one if (value = = Arr[i]) {return }15 return-1 }18}

Operation of the Java language Base 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.