13 Ways to manipulate the "Java" array

Source: Internet
Author: User
Tags addall set set

The content comes from StackOverflow. It may be too simple for some great gods, but here are a lot of things that I really used and learned for the first time.


0. define an 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 are initialized at the same time when allocating memory.


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]

This highlights the difference between the Java array value and the reference, the third line of code directly printed Intarray output is garbled, because Intarray is just a reference address, the fourth line of code output is the real array value, because it passed the arrays.tostring () The transformation.


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 would you want to convert an array to ArrayList? Maybe because ArrayList is a dynamic link list, we can more convenient to add and delete ArrayList, we do not need to loop array to each element added to the ArrayList, You can use the above code to convert.


3. Check whether an element is contained in an array

String[] Stringarray = {"A", "B", "C", "D", "E"};boolean B = arrays.aslist (Stringarray). Contains ("a"); System.out.println (b);//True
The array is converted to Arraylist<string> first through the Arrays.aslist () method, and then the contains () method is used 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. declaring an array within a 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

You can also use the join () method in Stringutil to output the elements in the array as strings.


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 and then convert the list to set.


8. array Inversion

Int[] Intarray = {1, 2, 3, 4, 5}; Arrayutils.reverse (Intarray); System.out.println (arrays.tostring (Intarray));//[5, 4, 3, 2, 1]
the same use of powerful arrayutils.


9. Removing an element from an array

Int[] Intarray = {1, 2, 3, 4, 5};int[] removed = Arrayutils.removeelement (Intarray, 3);//create a new ARRAYSYSTEM.OUT.PR Intln (arrays.tostring (removed));

Ten. converting 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);}


One by one . Copying an array

Int[] IntArray1 = {1,2,3,4,5};int[] intArray2 = new INT[5]; System.arraycopy (intArray1, 0, intArray2, 0, intarray2.length);

The first parameter is the source string, the second parameter represents where to start copying from the source array, the third parameter is the destination array, the fourth parameter represents the starting position where the copied content is placed in the destination array, and the fifth parameter represents the length of the copied content.


sorting an array

double[] num = {1.0, 5.2, 3.7, 2.9, 4.2};java.util.arrays.sort (num);

There are many useful methods in the arrays class.




13 Ways to manipulate the "Java" array

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.