Java Foundation--arrays Tools Class Ten common methods

Source: Internet
Author: User

Ten common methods of arrays tool class

Original link: http://blog.csdn.net/renfufei/article/details/16829457

0. Declaring an array
    New String[5];       = {"A", "B", "C", "D", "E"};       New String[]{"A", "B", "C", "D", "E"};  
1. Print an array
    int [] Intarray = {1, 2, 3, 4, 5 };       = arrays.tostring (intarray);              // print directly, the hash value      of the referenced object is printed // [email protected  ]    System.out.println (intarray);             // [1, 2, 3, 4, 5]      System.out.println (intarraystring);  
2. Create ArrayList based on an array
    String[] Stringarray = {"A", "B", "C", "D", "E" };      ArrayListNew arraylist<string>(arrays.aslist (Stringarray));       // [A, B, C, D, E]      System.out.println (arrayList);  
3. Check if the array contains a value
    String[] Stringarray = {"A", "B", "C", "D", "E" };       Boolean b = arrays.aslist (Stringarray). Contains ("a");       // true      System.out.println (b);  
4. Merge connections two arrays
    int [] Intarray = {1, 2, 3, 4, 5 };       int [] IntArray2 = {6, 7, 8, 9, ten };       // Apache Commons Lang gallery      int [] Combinedintarray = Arrayutils.addall (Intarray, intArray2);  
5. Declaring an inline array
    Method (new string[]{"A", "B", "C", "D", "E"});  
6. Concatenate (join) array with the given string
    // containing the provided list of elements       // Apache Common lang      String j = stringutils.join (new string[] {"A", "B", "C"}, ",");       // A, B, C      System.out.println (j);  
7. Convert ArrayList 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);  
8. Converting an array to a set
    New Hashset<string>(Arrays.aslist (Stringarray));       // [D, E, B, C, a]      SYSTEM.OUT.PRINTLN (set);  
9. Array element inversion
    int [] Intarray = {1, 2, 3, 4, 5 };      Arrayutils.reverse (intarray);       // [5, 4, 3, 2, 1]      System.out.println (arrays.tostring (Intarray));  
10. Removing elements
    int [] Intarray = {1, 2, 3, 4, 5 };       int [] removed = Arrayutils.removeelement (Intarray, 3); // Create a new array      System.out.println (arrays.tostring (removed));  
more--Convert int value to byte array
    byte [] bytes = Bytebuffer.allocate (4). Putint (8). Array ();               for (byte  t:bytes) {         System.out.format ("0x%x", T);      }  

Related reading:

    1. Example of sorting Arrays
    2. Read bytes from FileInputStream in Java
    3. Java:convert a file to a byte array, and then convert byte array to a file.
    4. Top Ten questions about Java collections

Java Foundation--arrays Tools Class Ten 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.