Array manipulation in Java

Source: Internet
Author: User

Objective

In Java, there are many well-packaged classes that can be used to manipulate arrays (sorting, copying, and so on), making arrays very convenient to use. This is the benefit of a high-level language.

code example-one-dimensional array

 Packagetest;Importjava.util.Arrays; Public classTest { Public Static voidMain (string[] args) {int[]a = {1, 3, 5, 2, 4, 6}; //convert an array to a string outputSystem.out.println (Arrays.tostring (a));/*//Sort the array after output arrays.sort (a);                System.out.println (Arrays.tostring (a));        Finds the subscript System.out.println of an element in an array (Arrays.binarysearch (A, 1));                System.out.println (Arrays.binarysearch (A, 5));        Copy an array of int []b = arrays.copyof (A, a.length);                System.out.println (arrays.tostring (b));        Sets all array elements to a value of Arrays.fill (b, 9);                System.out.println (arrays.tostring (b));        Compare two arrays of System.out.println (Arrays.equals (A, b));        Arrays.fill (A, 9); System.out.println (Arrays.equals (A, b));*/        }}

code example-two-dimensional array

1  Packagetest;2 3 Importjava.util.Arrays;4 5  Public classTest {6 7      Public Static voidMain (string[] args) {8         9         //statically createdTen         int[][]a = { One{1, 3, 5, 2, 4, 6}, A{2, 4, 6, 1, 3, 5} -                 }; -          the         //dynamically created -         introw = 2; -         intCol = 6; -         int[][]B =New int[Row][col]; +          -         //convert an array to a string output + System.out.println (Arrays.deeptostring (a)); A System.out.println (arrays.deeptostring (b)); at  -         } -}

Run results

Slightly.

Summary

1. Be aware of the dynamic creation of the above two-dimensional array. The meaning of this code differs from that in C + +-C + + can only create a one-dimensional pointer array dynamically, and then a new one-dimensional array on the pointer element of each pointer array. Here is the direct new out of the two-dimensional array.

2. There are many convenient APIs that are not listed in detail, so consult the Java API manual when using it.

Array manipulation 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.