The basic operation method of Java array sorting _java

Source: Internet
Author: User
Tags addall array length

An array is a set of data that has the same data type. Java supports multiple arrays, each base cell of a one-dimensional array is data of the basic data type, and the two-dimensional array is a one-dimensional array of one-dimensional arrays, and so on, each basic unit of an n-dimensional array is an array of n-1 dimensions that are n-1 arrays. The following is an example of a one-dimensional array to illustrate the use of Java arrays.

1. Array declaration

The array declaration has the following two forms (the square brackets differ in position):

int arr[];
Int[] ARR2;

2, array initialization

There are also two forms of array initialization, as follows (using new or not using new):

int arr[] = new Int[]{1, 3, 5, 7, 9};
Int[] arr2 = {2, 4, 6, 8, 10};

3, traversing the array

The traversal array can be For/foreach, as follows:

  public static void Main (string[] args) {
    int arr[] = new Int[]{1, 3, 5, 7, 9};
    Int[] arr2 = {2, 4, 6, 8, ten};
    for (int i = 0; i < arr.length ++i) {
      System.out.print (Arr[i] + "T");//1 3 5 7 9
    } for
    (int x:arr2) {
      System.out.print (x + "T");//2 4 6 8
    }
  }

4, Arrays.fill () Fill array

Using the static method of the Arrays class, import package java.util.Arrays is required, and many overloaded methods are defined.

void Fill (int[] A, int val) all fills 
void Fill (int[] A, int fromindex, int toindex, int val) fills the element of the specified index

    int[] Arr3 = new INT[5];
    for (int x:arr3) {
      System.out.print (x + "T");//0 0 0 0 0 all initialized to 0
    }
    System.out.println ();
    Arrays.fill (ARR3);
    for (int x:arr3) {
      System.out.print (x + "T");//10 10 10 10 10 all filled with the
    System.out.println ();
    Arrays.fill (ARR3, 1, 3, 8);
    for (int x:arr3) {
      System.out.print (x + "T");//10 8 8 10 10 populate the specified index
    }
    System.out.println ();

5, Arrays.sort () array sorting

void sort (int[] a) all sort 
void sort (int[] A, int fromindex, int toindex) Sorts the elements of the specified index

    int[] Arr4 = {3, 7, 2, 1, 9};
    Arrays.sort (ARR4);
    for (int x:arr4) {
      System.out.print (x + "T");//1 2 3 7 9
    }
    System.out.println ();
    Int[] Arr5 = {3, 7, 2, 1, 9};
    Arrays.sort (ARR5, 1, 3);
    for (int x:arr5) {
      System.out.print (x + "T");//3 2 7 1 9
    }
    System.out.println ();

6, arrays.copyof () copy array

Int[] copyof (int[] original, int newlength) copies the array, specifying the new array length 
int[] Copyofrange (int[] original, int from, int to) copies the array, Specifies the index of the original array being copied

    int[] arr6 = {1, 2, 3, 4, 5};
    int[] arr7 = arrays.copyof (ARR6, 5); 1 2 3 4 5
    int[] arr8 = Arrays.copyofrange (ARR6, 1, 3);//2 3 for
    (int x:arr7) {
      System.out.print (x + "T" );
    }
    System.out.println ();
    for (int x:arr8) {
      System.out.print (x + "T");
    }
    System.out.println ();

7, check whether 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 () to convert an array to List<string>, so that you can use the contains function of a dynamic list to determine whether an element is contained in a list.

8. Connect two arrays

Int[] Intarray = {1, 2, 3, 4, 5};
Int[] IntArray2 = {6, 7, 8, 9, ten};
Apache Commons Lang Library
int[] Combinedintarray = Arrayutils.addall (Intarray, intArray2);

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

9. Array Flip

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.

10. Remove an element from the array

Int[] Intarray = {1, 2, 3, 4, 5};
int[] removed = Arrayutils.removeelement (Intarray, 3);//create a new array
System.out.println (arrays.tostring ( removed));

Related Article

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.