Java_010 Java array


1. What is an array?

  • An array is a collection of combinations of elements with the same data type, and it can be applied using a common name.
  • Array can be defined as any type, can be one-dimensional or multi-dimensional.
    In particular, the elements in the array are accessed through the following table.

Second, the characteristics of the array

  • Is a collection of elements of the same data type.
  • The elements in the array are in order, and they are stored together in memory in this order.
  • Each array element is expressed by the name of the entire array and its ordinal position in the array.

The definition form is generally as follows:

int arr[] = new int[10];
int []arr = new int[10];
int[] arr = new int[10];

Array element type Array name [];
Array element type [] Array name;
Array element type [] Array name;
Or write in another form:

int arr[] = null;
arr = new int [10];
int []arr = null;
arr = new int[10];
int[] arr = null;
arr = new int[10];

1. Static initialization of the array

code show as below:
1.

//An improved version of the previous program.
 class AutoArray
 public static void main(String[] args) {
    
    
        int month_days[] = {
    
    31, 28, 31, 30, 31, 30, 31, 31, 31, 30, 31, 30, 31, 30, 31};
        System.out.println("April has "+ month_days[3] + " days.");
    }

2.

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        int[] nums;//声明一个数组

        nums = new int[10];//这里可以存储10个数据

        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        nums[5] = 6;
        nums[6] = 7;
        nums[7] = 8;
        nums[8] = 9;
        nums[9] = 10;
        //给数组中的元素赋值
        System.out.println(nums[9]);
        //计算所有元素的和
        int sum = 0;
        for (int i = 0; i < nums.length; i++){
    
    
            sum = sum + nums[i];
        }

        System.out.println("总和为"+sum);
    }
}

important point:

  • Java rigorously checks to ensure that values ​​outside the range of the array will not be accidentally stored or referenced.
  • The Java runtime system will check to ensure that all array subscripts are within the correct range (in this respect, Java is fundamentally different from C and C++)
  • When declaring an array in the Java language, no matter how you define the array, you cannot specify its length. For example, the following definition will be illegal.
int ia[5]; //编译时将出错

Three, multi-dimensional array

1. Multidimensional array

definition

Multidimensional arrays are actually arrays of arrays, But these arrays are not exactly the same in form and behavior.

1. The following statement defines a two-dimensional array variable named twoD:

int twoD[][] = new int[4][5] 

Code example:

public class TwoDArray {
    
    
    public static void main(String[] args) {
    
    
        int twoD[][] = new int[4][5];
        int i, j, k = 0;
        for (i = 0; i < 4; j++) {
    
    
            twoD[i][j] = k;
            k++;
        }
    }
     for (i = 0; i <4; i++){
    
    
        for (j = 0; j < 5; j++) {
    
    
            System.out.println(twoD[i][j] + " ");
        }
       System.out.println();
    }
}

Fourth, the related methods of array operations

The following describes two of the related methods of array operations:

1. Use the System.arraycopy() method to copy the array

System.arraycopy(sourse, 0, dest, 0, x);

The meaning of this statement is: copy the x elements starting from subscript 0 in the source array to the destination array, and store from the position corresponding to the subscript 0 of the target array.
Sample program:

public class example_08 {
    
    
    public static void main(String[] args) {
    
    
        int aLength = 2;
        int[] arryA = new int[]{
    
    0, 1, 2, 3, 4, 5, 6, 7};
        int[] arryB = new int[]{
    
    33, 44, 55};
        System.arraycopy(arryA, 0, aLength, 0, 2);
        }
    }

Reference running results:

arrayB: 0, 1, 55

note:
The number of elements in the copied array must be less than the length of the destination array, otherwise an exception will occur.

2. Use Arrays.sort() to sort the elements in the array

The following example is to randomly generate 10 random numbers and arrange them in ascending order, and then print the sorted array:

import java.util.Arrays;

public class example_08_01 {
    
    
    public static void main(String[] args) {
    
    
        int[] num = new int[10];//可以根据自己的需要定义产生几个随机数
        System.out.println("产生的随机数为:");
        for (int i=0; i<10; i++){
    
    
            num[i] = (int)(Math.random()*100);
            System.out.println(num[i]+" ");
        }
        System.out.println();
        Arrays.sort(num);
        System.out.println("排序后的随机数为:");
        for (int i= 0; i<10; i++){
    
    
            System.out.println(num[i]+" ");
        }

    }
}

One of the results of the run:

产生的随机数为:
65 
91 
20 
99 
26 
9 
68 
34 
76 
91 

排序后的随机数为:
9 
20 
26 
34 
65 
68 
76 
91 
91 
99 

The picture comes from Meet the Crazy God

Guess you like

Origin blog.csdn.net/weixin_49207937/article/details/114604039