Common methods for arrays in Java

Source: Internet
Author: User

0. Create/Declare an array

123 String[] aArray = new String[5];String[] bArray = {"a","b","c", "d", "e"};String[] cArray = new String[]{"a","b","c","d","e"};

  

Print an array in 1.Java

123456789 int[] intArray = { 1, 2, 3, 4, 5};String intArrayString = Arrays.toString(intArray); // print directly will print reference valueSystem.out.println(intArray);// [[email protected]System.out.println(intArrayString);// [1, 2, 3, 4, 5]

 

2. Create a ArrayList with an array

1234 String [ ] stringArray = { "a", "b" , "c" , "d" , "e" } ; ArrayList < String > arrayList = newArrayList < String > ( Arrays . asList ( stringArray ) ) ; System . out . println ( arrayList ) ; // [A,B,C,D,E]

 

3, check whether the array contains a specific value

123 String[] stringArray = { "a", "b", "c", "d", "e"};booleanb = Arrays.asList(stringArray).contains("a");System.out.println(b);

  

4. Combining two arrays

1234 int[] intArray = { 1, 2, 3, 4, 5 };int[] intArray2 = { 6, 7, 8, 9, 10 };// Apache Commons Lang libraryint[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

  

5. Methods for declaring an array

1 method(newString[]{"a", "b", "c", "d", "e"});

  

6, joins the elements in the supplied array into a string

12345 // containing the provided list of elements// Apache common langString j = StringUtils.join(newString[] { "a", "b", "c"}, ", ");System.out.println(j);// a, b, c

 

7. Conversion between array and list

123456 string[] Stringarray = { Code class= "Java plain", "C" "D" "E" arraylist<string> ArrayList = new arraylist<string> (arrays.aslist (Stringarray)); string[] Stringarr = new string[arraylist.size ()]; arraylist.toarray (Stringarr); for system.out.println (s);

 

8. Array conversion to set

123 Set<String> set = newHashSet<String>(Arrays.asList(stringArray));System.out.println(set);//[d, e, b, c, a]

  

9. Array reverse output

1234 int[] intArray = { 1, 2, 3, 4, 5};ArrayUtils.reverse(intArray);System.out.println(Arrays.toString(intArray));//[5, 4, 3, 2, 1]

  

10. Deleting an array element

123 int[] intArray = { 1, 2, 3, 4, 5};int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new arraySystem.out.println(Arrays.toString(removed));

 

Finally, int is converted into a byte array

12345 byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();for (bytet : bytes) {System.out.format("0x%x ", t);}

Common methods for arrays in Java

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.