Basic manipulation of Java arrays

  • 2020-04-01 04:09:23
  • OfStack

An array is a collection of a set of data with the same data type, Java support for arrays, each basic unit is the basic of one-dimensional array data types of data, a two-dimensional array is each basic unit is a one-dimensional array of one dimensional array, and so on, n-dimensional array of each basic unit is n - 1 for an array of n - 1 d array. Let's take a one-dimensional array as an example to illustrate the use of Java arrays.

1. Array declaration

Array declarations can take the following two forms (with different positions of square brackets) :


int arr[];
int[] arr2;

2. Array initialization

Array initialization can also take two forms, as follows (with or without new) :


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

3. Go through the groups

Traversal groups can be used 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, 10};
    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 10
    }
  }

4. Arrays.fill() fills the array

Using the static methods of the Arrays class requires the import package java.util.Arrays, which defines many overloaded methods.


void fill(int[] a, int val) All filled  
void fill(int[] a, int fromIndex, int toIndex, int val) Populates the element of the specified index 

    int[] arr3 = new int[5];
    for (int x: arr3) {
      System.out.print(x + "t"); //Zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero zero
    }
    System.out.println();
    Arrays.fill(arr3, 10);
    for (int x: arr3) {
      System.out.print(x + "t"); //10, 10, 10, 10, 10, all filled with 10
    }
    System.out.println();
    Arrays.fill(arr3, 1, 3, 8);
    for (int x: arr3) {
      System.out.print(x + "t"); //10, 8, 8, 10, 10 populates the specified index
    }
    System.out.println();

5. Arrays.sort() sorts Arrays


void sort(int[] a) All sorts  
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 (


int[] copyOf(int[] original, int newLength) Copies the array, specifying the new array length  
int[] copyOfRange(int[] original, int from, int to) Copies an array, specifying 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 certain value


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

First, use arrays.aslist () to convert the Array to List< String> , so you can use the contains function of the dynamic list to determine whether an element is contained in the list.

8. Join two arrays


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

ArrayUtils is an array-processing class library provided by Apache whose addAll method makes it easy to join two arrays into one.

9. Flip the array


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

Still USES the versatile ArrayUtils.

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 articles: