JAVA Arrays and Common methods

Source: Internet
Author: User

1. Defining arrays

String [] A;

String [] a=new int[5];

int a[]=new int[5];

Int[] a =new int[]{1,2,3,4}; two-dimensional array: int [] [] a=new int[2 (must write)][2 (not writable)] The first 2 is the length of the two-dimensional array, and the second 2 is the length of a single object in a two-dimensional array, and you can not write 2. Gets the array length int [ ] A=new int[]{1,2,3,4}int b=a.length; 3. Fill Array (1) fill int [] a=new int [5]; Arrays.fill (a,4); array a changes to {4,4,4,4,4} method: Arrays.fill (array name, content to populate); Note: The Fill method fills the content with the content to be populated. (2) partially filled int [] a =new int [7]; Arrays.fill (a,0,3,9); array a becomes {9,9,9,0,0,0,0} method: Arraysfill (array name, populate data start index, fill data end index +1/actual calculation is needed-one to get index, populate content); 4. Copy Array (1) copy all int [] a=new int [] {1,2,3,4};int [] a1=arrays.copyof (a), array A1 to {1,2,3,4} (2) partially copy int [] a=new int [] {1,2,3,4}; int [] A1=arrays.copyofrange (a,1,2); The result of the array A1 becomes {2} NOTE: Here the first number is the start index, the second number is the end index +1 (in the actual calculation need to treat the number 1, to obtain the end index), 5. Compare two array int [] a=new int []{1,2,3};int [] b=new int []{1,2,3}; System.out.println (Arrays.equals (A, A, b); the result is true; 6. Determine if an element is contained in an array depending on the actual situation, the method of traversal or dichotomy is chosen. The traverse efficiency is slightly higher, the code is a bit more. Two the efficiency of the method is slightly lower, the single code is small.
 Public classText { Public Static voidMain (string[] args) {string[] a=NewString[] {"AVFG", "BCD", "4s", "de", "1d1", "2dff2", "4gsd4", "4SDFS1", "+", "SDF" }; String b= "1D1"; Boolean D=false;  for(String c:a) {if(C.equals (b)) {D=true;        }} System.out.println (d); /*This is dichotomy * arrays.sort (a); Sort first ...         Necessary!!!!         * int c = Arrays.binarysearch (A, "1d1");         * SYSTEM.OUT.PRINTLN (c); The output value is greater than or equal to 0 proof there, for the negative there is no!! */    }}

JAVA Arrays and Common methods

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.