12 best Practices for Java arrays

Source: Internet
Author: User


1. Declaring an array

    New String[5];       = {"A", "B", "C", "D", "E"};       New String[]{"A", "B", "C", "D", "E"};  


2. Output an array

    int [] Intarray = {1, 2, 3, 4, 5 };       = arrays.tostring (intarray);              // Print directly would print reference value       System.out.println (intarray);       // [email protected  ]           System.out.println (intarraystring);       // [1, 2, 3, 4, 5]  


3. Creating an array list from an array

String[] Stringarray = {"A", "B", "C", "D", "E" }; ArrayListNew arraylist<string>(arrays.aslist (Stringarray)); System.out.println (arrayList); // [A, B, C, D, E]


4. Check if an array contains a value

    String[] Stringarray = {"A", "B", "C", "D", "E" };       Boolean b = arrays.aslist (Stringarray). Contains ("a");      System.out.println (b);       // true  


5. Connect two arrays

    int [] Intarray = {1, 2, 3, 4, 5 };       int [] IntArray2 = {6, 7, 8, 9, ten };       // Apache Commons Lang library      int [] Combinedintarray = Arrayutils.addall (Intarray, intArray2);  


6. Declares an inline array (array inline)

    Method (new string[]{"A", "B", "C", "D", "E"});  


7. Put the supplied array elements into a string

    // containing the provided list of elements       // Apache Common lang      String j = stringutils.join (new string[] {"A", "B", "C"}, ",");      System.out.println (j);       // A, B, C  


8. Converting an array list to arrays

    String[] Stringarray = {"A", "B", "C", "D", "E" };      ArrayListNew arraylist<string>(arrays.aslist (Stringarray));       New string[arraylist.size ()];      Arraylist.toarray (Stringarr);        for (String S:stringarr)          System.out.println (s);  


9. Converting an array to set (set)

    New Hashset<string>(Arrays.aslist (Stringarray));      SYSTEM.OUT.PRINTLN (set);       // [D, E, B, C, a]  


10. Reverse an array

    int [] Intarray = {1, 2, 3, 4, 5 };      Arrayutils.reverse (intarray);      System.out.println (arrays.tostring (Intarray));       // [5, 4, 3, 2, 1]  


11. Move the elements in the divisor group

int [] Intarray = {1, 2, 3, 4, 5 }; int [] removed = Arrayutils.removeelement (Intarray, 3); // Create a new array System.out.println (arrays.tostring (removed));


12. Converting integers to byte arrays

    byte [] bytes = Bytebuffer.allocate (4). Putint (8). Array ();               for (byte  t:bytes) {         System.out.format ("0x%x", T);      }  

12 best Practices for Java arrays

Related Article

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.