Brief description of Java arrays

Array

1. One-dimensional array
1, what is an array? The
array itself is a variable, but the array stores a set of data of the same data type.

 声明一个变量,就是在内存中划分出一块合适的空间;
 声明一个数组,就是在内存中划分出一连串连续的空间

2. Why do we need arrays?
When we need to store a series of data, we need to use arrays. If we don't use arrays, we need to declare variables one by one, which wastes memory space and is also inefficient.

3. The basic elements of the array:
identifier (array name)
array element (data stored in the array)
element subscript: (how to find the element inside) starting from 0, each element can be subscripted to
find the
element type ( The data type stored in the array is also the type of the
array) Once the length of the array is declared, it is fixed. If the declared length is 4, only four data can be stored.
If the stored data exceeds the length of the array, it will prompt that the array is out of bounds.

Create an array and assign values ​​must be written in the same sentence; the
relationship between the length of the array and the subscript, the subscript is from 0--------(array length -1);

If the array is not full,
the default is 0
if the array is of int data type, if the array is of double data type, the default is 0,
if the array is of String data type, the default is null

Example:
define an array, output its length and elements:
code:

package test;

public class test07 {
    
    
    public static void main(String[] args) {
    
    
        int num [] ={
    
    1,2,3,4};
        System.out.println("数组长度:"+num.length);
        for (int i : num) {
    
    
            System.out.println(i);
        }
    }
}

Result display:
Insert picture description here

Second, two-dimensional array
1, what is a two-dimensional array? A
two-dimensional array is to store an array in the array

2. Syntax
Data type [][] array name; or data type array name [][ ];

Memory: artificially divided into stack memory and heap memory.
Stack memory is used to store variable names.
Heap memory is used to store data.

Example:
int [][] scores; //Define a two-dimensional array
scores=new int [5][50]; //Allocate memory space

//Or
int [][]scores = new int[5][50];

Note : When
defining an array, the length of the peripheral array must be defined, that is, the maximum dimension is defined. The length of the two arrays inside and outside cannot be set.
For example: int[][]scores = new int [5][]; Yes
int[][]scores = new int [][]; No

3. Memory Figure
Insert picture description here
4, Example
Define a two-dimensional array, view the elements in the first row according to the user's keyboard input, display all the elements in the row, and write any data. As shown in the figure:
Insert picture description here

Code display

package zuoye;

import java.util.Scanner;

public class t02 {
    
    
    public static void main(String[] args) {
    
    
        int [][]nums={
    
    {
    
    1,2,3},{
    
    4,5,6},{
    
    7,8,9}};
        int[] t = new int[3];
        for (int i = 0; i <nums.length ; i++) {
    
    
                if (i==nums.length-1){
    
    
                    System.out.println("当前数组有"+(i+1)+"行,您想查看第几行的元素,请输入:");
                }
        }
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        for (int i = 0; i <nums.length ; i++) {
    
    
            if (a==i+1){
    
    
                for (int j = 0; j <nums[i].length ; j++) {
    
    
                    System.out.println(nums[i][j]);
                }
            }
        }

    }
}

operation result:
Insert picture description here

Summarize so much first, and I will add it later

Guess you like

Origin blog.csdn.net/tan1024/article/details/109862121