Java Array Tips

Source: Internet
Author: User

Java Array Tips

0. Declare an array (Declare an array)

[JS]View Plaincopyprint?
    1. string[] Aarray = new string[5];
    2. String[] Barray = {"a","B","C", "D", "E"};
    3. string[] CArray = new string[]{"a","B","C","D","E"};
string[] Aarray = new STRING[5]; String[] Barray = {"A", "B", "C", "D", "E"}; string[] CArray = new string[]{"A", "B", "C", "D", "E"};

1. Output an array from Java (print an array in Java)

[JS]View Plaincopyprint?
    1. int[] intarray = { 1, 2,  3, 4, 5 };  
    2. string intarraystring = arrays.tostring ( Intarray);   
    3.    
    4. // print  directly will print reference value  
    5. System.out.println (intarray);   
    6. // [[email protected]   
    7.    
    8. System.out.println (intarraystring);  
    9. // [1, 2, 3, 4, 5]  
Int[] Intarray = {1, 2, 3, 4, 5}; String intarraystring = arrays.tostring (Intarray); Print directly would print reference valueSystem.out.println (intarray);//[Email protected] SYSTEM.OUT.PRINTLN ( intarraystring);//[1, 2, 3, 4, 5]

2. Create an array list from the array (create a ArrayListfrom an array)

[JS]View Plaincopyprint?
    1. String[] Stringarray = { "a", "B", "C", "D", "E"};
    2. arraylist<string> ArrayList = new Arraylist<string> (Arrays.aslist (Stringarray));
    3. System.out.println (arrayList);
    4. [A, B, C, D, E]
String[] Stringarray = {"A", "B", "C", "D", "E"}; arraylist<string> ArrayList = new Arraylist<string> (arrays.aslist (Stringarray)); System.out.println (arrayList);//[A, B, C, D, E]

3. Check if the array contains a specific value (check if an array contains a certain value)

[JS]View Plaincopyprint?
    1. String[] Stringarray = { "a", "B", "C", "D", "E"};
    2. Boolean B = Arrays.aslist (Stringarray). Contains ("a");
    3. System.out.println (b);
    4. True
String[] Stringarray = {"A", "B", "C", "D", "E"};boolean B = arrays.aslist (Stringarray). Contains ("a"); System.out.println (b);//True

4. Connect two arrays (concatenate two arrays)

[JS]View Plaincopyprint?
    1. Int[] Intarray = {1, 2, 3, 4, 5};
    2. Int[] IntArray2 = {6, 7, 8, 9, 10};
    3. Apache Commons Lang Library
    4. int[] Combinedintarray = Arrayutils.addall (Intarray, intArray2);
Int[] Intarray = {1, 2, 3, 4, 5};int[] IntArray2 = {6, 7, 8, 9,};//Apache Commons Lang libraryint[] Combinedintarr ay = Arrayutils.addall (Intarray, intArray2);

5. Declare an array of inline chains (Declare an array inline)

[JS]View Plaincopyprint?
    1. Method (new string[]{"a", "B", "C", "D", "E"});
Method (New string[]{"A", "B", "C", "D", "E"});

6. Add an array element to a separate string (Joins the elements of the provided array into a single string)

[JS]View Plaincopyprint?
    1. Containing the provided list of elements
    2. Apache Common Lang
    3. String j = stringutils.join (new string[] { "a", "B", "C"}, ",");
    4. System.out.println (j);
    5. A, B, c
Containing the provided list of elements//Apache common langstring j = stringutils.join (new string[] {"A", "B", "C"} , ", "); System.out.println (j);//A, B, c

7. Converting an array list into an array (Covnert a ArrayList to an array)

[JS]View Plaincopyprint?
    1. String[] Stringarray = { "a", "B", "C", "D", "E"};
    2. arraylist<string> ArrayList = new Arraylist<string> (Arrays.aslist (Stringarray));
    3. string[] Stringarr = new String[arraylist.size ()];
    4. Arraylist.toarray (Stringarr);
    5. for (String S:stringarr)
    6. System.out.println (s);
String[] Stringarray = {"A", "B", "C", "D", "E"}; arraylist<string> ArrayList = new Arraylist<string> (arrays.aslist (Stringarray)); string[] Stringarr = new String[arraylist.size ()];arraylist.toarray (Stringarr); for (String S:stringarr) System.out.println (s);

8. Convert an array into a collection (convert an array to a set)

[JS]View Plaincopyprint?
    1. set<string> set = new Hashset<string> (Arrays.aslist (Stringarray));
    2. SYSTEM.OUT.PRINTLN (set);
    3. [D, E, B, C, a]
set<string> set = new Hashset<string> (arrays.aslist (Stringarray)); SYSTEM.OUT.PRINTLN (set);//[d, E, B, C, a]

9. Inverse Array (Reverse an array)

[JS]View Plaincopyprint?
    1. Int[] Intarray = {1, 2, 3, 4, 5};
    2. Arrayutils.reverse (Intarray);
    3. System.out.println (arrays.tostring (Intarray));
    4. [5, 4, 3, 2, 1]
Int[] Intarray = {1, 2, 3, 4, 5}; Arrayutils.reverse (Intarray); System.out.println (arrays.tostring (Intarray));//[5, 4, 3, 2, 1]

10. Delete the array element (remove element of an array)

[JS]View Plaincopyprint?
    1. Int[] Intarray = {1, 2, 3, 4, 5};
    2. int[] removed = Arrayutils.removeelement (Intarray, 3); //create a new array
    3. System.out.println (arrays.tostring (removed));
Int[] Intarray = {1, 2, 3, 4, 5};int[] removed = Arrayutils.removeelement (Intarray, 3);//create a new ARRAYSYSTEM.OUT.PR Intln (arrays.tostring (removed));

One more–convert int to byte array

[JS]View Plaincopyprint?
    1. byte[] bytes = bytebuffer.allocate (4). Putint (8). Array ();
    2. For (byte t:bytes) {
    3. System.out.format ("0x%x", t);
    4. }

Java Array Tips

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.