Java Basics (V): Array

Array, a very extensive data structure, it is simply a set of the same type and disorder of elements stored in a fixed length and ordered memory space.

Create an array

In Java, we can pass []a statement to specify the type of array

int[] a; // 写法一
int a[]; // 写法二

Of course, in general, we prefer to use the first way to declare an array, because it separates the type of a variable name to optimize the readability of the code.
We just declare an array just a, but not to a initialized to a true array.

When assigned to the array, we can in three ways

int[] a = new int[4];
int[] a = new int[]{1,2,3,4};
int[] a = {1,4,3,2}

Which is actually the second third of shorthand, we can go to create an anonymous array by using the new keyword

new int[4];

But remember to specify the length of the element or the specified array, an anonymous array If you want to create here, new keywords is essential

{1,2,4,3} // 这样写是错误的!

Whether we how to define an array, its length in the beginning of creation is to be determined, but note that its length is not endless, we can see the reflection in the package Arrayto get its length data source category Types of:

public static Object newInstance(Class<?> componentType, int length)
        throws NegativeArraySizeException {
        return newArray(componentType, length);
}

Here you can see the data type is an array of type int int type mentioned in the foregoing we have, its maximum length is \ (2 ^ {31} \) , i.e. 2GB.

Access elements in the array

We can be accessed by way of elements in the array subscript, and array indices start at 0, the maximum length is the length of the array, if we access data beyond the standard range under an array, an exception will be thrown index out of bounds (ArrayOutOfIndexError), because we can subscript directly access elements in the array, so the time complexity is O (1).

int[] a = {1,2,3};
System.out.println(a[0]); // 1

Add elements to an array

We just said that the length of the array is fixed, we were unable to change the structure of the array, but we can achieve this effect by another method

        int[] arr = {9,7,5};
        int[] temp = new int[arr.length+1];
        for(int i = 0;i < arr.length;i++) { 
            temp[i]=arr[i];
        }
        temp[arr.length] = 6;
        arr = temp;

Let's analyze Paint

image-20190826222706460

Removing elements

And new, deleting elements in the array is also not allowed, we can complete the operation to delete and add a similar way by

int[] arr = { 1, 2, 3, 4, 5};
int[] tmp = new int[arr.length - 1];
for (int i = 0; i < tmp.length; i++) {
  tmp[i] = arr[i];
}
arr = tmp;

The new principle and are relatively similar, here I will not go detailed description of the drawing

Two-dimensional array

We like to create a one-dimensional array can still create a two-dimensional array

int[][] doubleArr = new int[2][3];
int[][] doubleArr = {{1,2,3,4},{5,6,7,8}};
int[][] doubleArr = new int[5][];

It should be noted that, when you create a two-dimensional array, you can specify the length of a dimension, without specifying the length of the second dimension, so that dynamic changes. For instance, we can draw stars

String[][] arr = new String[5][];
for (int i = 0; i < arr.length; i++) {
  arr[i] = new String[i + 1];
  for (int j = 0; j < arr[i].length;j++) {
    arr[i][j] = "*";
  }
}

for (int i = 0; i < arr.length; i++) {
  for (int j = 0; j < arr[i].length;j++) {
    System.out.print(arr[i][j]);
  }
  System.out.println();
}

image-20190826231109164

the public

Guess you like

Origin www.cnblogs.com/viyoung/p/12521934.html