[Java Array Learning] Array Basics

One, the array

An array is an ordered collection of data of the same type .

The array describes several data of the same type, arranged and combined according to a certain sequence .

Among them, each data is called an array element , and each array element can be accessed through a subscript.

Second, the array declaration creation

1. The array variable must be declared first before the array can be used in the program. The following is the syntax for declaring number variables:

dataType[] arrayRefVar;   //首选的方法
或
dataType arrayTRefVar[];  //效果相同,但不是首选方法

2. Java language uses the new operator to create an array, the syntax is as follows:

dataType[] arrayRefVar = new dataType[arraySize];

3. The elements of the array are accessed by index, and the array index starts from 0.

4. Get the length of the array

arrays.length
public static void main(String[] args) {
    
    
    int[] nums;  //声明了一个数组
    nums = new int[10];//这里面可以存放10个类型的数字,声明和存放可以合成一步:  int[] nums =new int[10]
    //给数组元素中赋值
    nums[0]=0;
    nums[1]=1;
    nums[2]=2;
    nums[3]=3;
    nums[4]=4;
    nums[5]=5;
    nums[6]=6;
    nums[7]=7;
    nums[8]=8;
    nums[9]=9;
    System.out.println(nums[9]);
    
    //以下计算所有元素的和
    int sum =0;
    //获取数组长度:arrays。length
    for (int i=0;i<nums.length;i++) {
    
    
        sum = sum + nums[i];
    }
    System.out.println(sum);  //输出结果为45
    }

Three, memory analysis

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-YBSU11Sd-1615556003162)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20210312210020364.png)]

Four, three kinds of initialization

Static initialization
int[] a ={
    
    1,2,3};
Man[] mans ={
    
    new Man(1,1),new Man(2,2)};
Dynamic initialization
int[] a =new int[];
a[0]=1;
a[1]=2;
Default initialization of the array

Array is a reference type , and its elements are equivalent to instance variables of the class. Therefore, once space is allocated for the array, each element in it is implicitly initialized in the same way as instance variables. (All the initial values ​​before assignment are 0)

//静态初始化
int[] a ={
    
    2,3,4,52,3,6,5};
    System.out.println(a[0]);

    //动态初始化
    int[] b = new int[4];
    b[0] =10;
    b[1] =3;
    System.out.println(b[0]);//输出10
    System.out.println(b[3]);//输出初始值 0

Five, the four basic characteristics of arrays

  • Its length is determined. Once the array is created, its size cannot be changed .

  • The elements must be of the same type , and mixed types are not allowed.

  • The elements in the array can be any data type, including basic types and reference types .

  • Array variables are reference types , arrays can also be regarded as objects, and each element in the array is equivalent to a member variable of the object.

    Properties themselves are objects. Objects in Java are on the heap . Therefore, no matter whether the array saves primitive types or other object types, the array objects themselves are on the heap .

Group number boundary

  1. The legal range of the subscript: [0,length-1], an error will be reported if it crosses the boundary;

, 2. An error will be displayed: ArraylndexOutOfBoundsException: The array table below is out of bounds exception!

Guess you like

Origin blog.csdn.net/weixin_44302662/article/details/114708676