10 ways to manipulate Java arrays (GO)

Source: Internet
Author: User

0. Define a Java array
string[] Aarray = new STRING[5]; String[] Barray = {"A", "B", "C", "D", "E"}; string[] CArray = new string[]{"A", "B", "C", "D", "E"};

The first is to define an array and specify the length of the array, which we call the dynamic definition.

The second and third types also initialize values while allocating memory space.

1. Printing elements in a Java array
Int[] Intarray = {1, 2, 3, 4, 5}; String intarraystring = arrays.tostring (intarray);//print directly'll print reference valueSystem.out.println ( Intarray);//[Email protected]system.out.println (intarraystring);//[1, 2, 3, 4, 5]

The focus here is to illustrate the reference and merit of the array in Java, the third line directly prints Intarray, the output is garbled, because Intarray is only an address reference. The 4th line of output is the true array value, as it passes through the arrays.tostring () conversion. For Java beginners, references and values still need to be valued.

2. Create a ArrayList from the array
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]

Why convert an array to ArrayList? Perhaps because ArrayList is a dynamic linked list, we can more easily add and remove ArrayList, we do not need to loop array to each element added to the ArrayList, with the above code can be easily converted.

3. Check if the array contains a value
String[] Stringarray = {"A", "B", "C", "D", "E"};boolean B = arrays.aslist (Stringarray). Contains ("a"); System.out.println (b);//True

Use Arrays.aslist () first to convert the array to List<string>, so you can use the contains function of the dynamic list to determine whether the element is contained in the linked list.

4. Connect two arrays
Int[] Intarray = {1, 2, 3, 4, 5};int[] IntArray2 = {6, 7, 8, 9,};//Apache Commons Lang libraryint[] Combinedintarr ay = Arrayutils.addall (Intarray, intArray2);

Arrayutils is an array processing class library provided by Apache, and its AddAll method makes it easy to concatenate two arrays into an array.

5. Declare an array within the chain
Method (New string[]{"A", "B", "C", "D", "E"});
6. The elements in the array are exported as strings
Containing the provided list of elements//Apache common langstring j = stringutils.join (new string[] {"A", "B", "C"} , ", "); System.out.println (j);//A, B, c

Also, using the Join method in StringUtils, you can output the elements in the array as a string.

7. Converting an array into a set set
set<string> set = new Hashset<string> (arrays.aslist (Stringarray)); SYSTEM.OUT.PRINTLN (set);//[d, E, B, C, a]

Using set in Java makes it easy to save the required types in a variable in a collection type, primarily in a display list. You can also convert an array to a list before converting the list to set.

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

Still use the omnipotent arrayutils.

9. Remove an element from the array
Int[] Intarray = {1, 2, 3, 4, 5};int[] removed = Arrayutils.removeelement (Intarray, 3);//create a new ARRAYSYSTEM.OUT.PR Intln (arrays.tostring (removed));
Add one: Convert an int value to a byte array
byte[] bytes = bytebuffer.allocate (4). Putint (8). Array (); for (byte t:bytes) {System.out.format ("0x%x", t);}

Link: http://www.codeceo.com/article/10-java-array-method.html
The original: Top Methods for Java Arrays

10 ways to manipulate Java arrays (GO)

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.