Java is in hand, I have the world! ! ! Java-based arrays-the definition, classification and use of arrays

table of Contents

Preface

What: what is the array

Why: Why use arrays

How: How to use the array

Examples of array code


Preface

In the previous article, I took you to recall a method and method overloading in Java. Today I will take you to look at arrays in Java, which is a very basic knowledge point. The editor believes that water drop can only help Ishikawa. In addition to rich project experience, an excellent programmer also needs a solid foundation, step by step. I hope this article can help and inspire you who are watching.

What: what is the array

The concept of an array : an array is a container that can store multiple data values ​​of the same data type at the same time.

The characteristics of the array:

1. Array is a reference data type;

2. For multiple data in the array, the data type must be consistent;

3. The length of the array cannot be changed during operation.

Why: Why use arrays

Why use arrays? It depends on the use of variables. Generally speaking, we define a variable and generally only assign a value to it. If we want to assign a value to a variable of the same data type, we can also write: int a=5,b=7,c=10; but this is also a definition The three variables a, b, and c of type int are assigned to them respectively. Assuming that we want to count the scores of group members at this time, we can define 5 variables to receive scores for 5 people. But if it is required to count the grades of the whole class at this time, do you have to define 50 variables to accept the grades of the whole class? This obviously increases our workload. The array can solve this problem, as the above definition says: an array is a container that can store multiple data values ​​of the same data type at the same time.

How: How to use the array

Two common array initialization methods:

1. Dynamic initialization (specify length): When creating an array, directly specify the number of data elements in the array.

2. Static initialization (specify content): When creating an array, do not directly specify the number of data, but directly specify the specific data content.

Format of the initialization array:

1. Dynamic initialization format: data type [] array name = new data type [array length];

2. Static initialization format: data type [] array name = new data type [] {element 1, element 2, element 3,...};

The omission of statically initialized arrays:

Data type [] Array name = {element 1, element 2, element 3,...};

Note: Although static initialization does not directly tell the length, the length can be automatically calculated according to the specific content of the elements in the braces.

Examples of array definition codes:

public class Demo01Array {
    public static void main(String[] args) {
       
        //动态初始化数组:创建一个数组,里面可以存放300个int数据
        int[] arrayA=new int[300];

        //动态初始化数组:创建一个数组,里面可以存放5个字符串
        String[] arrayC=new String[5];

        //静态初始化数组:直接创建一个数组,里面装的全都是int数字,具体为:5  15  25
        int[] arrayInt=new int[]{5,15,25};

        //静态初始化数组:直接创建一个数组,里面装的全都是字符串,具体为:“Hello”  "World"  "Java"
        String[] arrayStr=new String[]{"Hello","World","Java"};

         //静态初始化数组的省略写法
        int[] arrayInt={1,5,9};
    }
}

Precautions for initializing and splitting the defined array:

1. The standard form of static initialization can be split into two parts;

2. The standard form of dynamic initialization can also be split into two parts;

3. Once the static initialization uses the omitted form, it cannot be split into two steps.

        //动态初始化的标准形式可以拆分成两部分
        int [] arrayA;
        arrayA=new int[5];

        //静态初始化的标准形式也可以拆分成两部分;
        int[] arrayB;
        arrayB=new int[]{5,7,9};

Examples of array code

Array default value: when using dynamic initialization array, the element will have a default value

 

public class Demo03ArrayUse {
    public static void main(String[] args) {
        String[] arrayA=new String[3];
        int[] arrayB=new int[5];

        //直接打印数组元素
        System.out.println(arrayA[0]);      //null
        System.out.println(arrayB[0]);       //0
}

 Array index: The index of the array starts from 0 and ends at "array length-1".

 Array common problem 1: If the index number does not exist when accessing the array element, then an array index out of bounds exception ArrayIndexOutOfBoundsException will occur .

public class Demo05ArrayIndex {
    public static void main(String[] args) {
        int[] arrayInt=new int[]{5,8,9};
        System.out.println(arrayInt[0]);        //5
        System.out.println(arrayInt[1]);        //8
        System.out.println(arrayInt[2]);        //9
        //错误代码:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        //System.out.println(arrayInt[3]);
    }
}

Array FAQ 2: All reference type variables can be assigned a null value. But it means nothing. The array must be initialized with new before the elements can be used. If only a null is assigned and no new creation is performed, then it will happen: NullPointerException

public class Demo06ArrayNull {
    public static void main(String[] args) {
        int[] array=null;
        //错误代码:空指针异常
        System.out.println(array[0]);
    }
}

Get the length of the array : array name.length

public class Demo07ArrayLength {
    public static void main(String[] args) {
        int[] arrayB={10,23,25,63,58,55,55,2,3,8,8,9,5,10,23,25,63,58,55,55,2,3,8,8,9,5};
        int len=arrayB.length;
        System.out.println(len);
    }
}

Array traversal: for loop to traverse the array

public class Demo08Arrayprint {
    public static void main(String[] args) {
        //定义一个数组
        int[] arrayInt={5,8,9,6,45,45,12,56,7};
        //数组的遍历
        for(int i=0;i < arrayInt.length;i++){
            System.out.println(arrayInt[i]);
        }
    }

Find the minimum value of the array (the same as the maximum value)

public class Demo10ArrayMin {
    public static void main(String[] args) {
        //求数组元素最小值
        int[] arrayInt=new int[]{7,80,90,80,-100};
        int min=arrayInt[0];
        for (int i = 1; i < arrayInt.length; i++) {
            if(arrayInt[i]<min){
                min=arrayInt[i];
            }
        }
        System.out.println("数组当中的最小值为;"+min);
    }
}

Inversion of array: see blog

The array is passed as a method parameter: when the method is called, the parameter is passed in the parentheses of the method, and the address value of the array is passed in.

public class Demo01ArrayParam {
    public static void main(String[] args) {
        int[] array={10,20,30,40,50};
        //调用此方法,传入一个数组作为参数
        printArray(array);
    }
    public static void printArray(int[] array){
        System.out.println("printArray方法收到的参数是:"+array);        //地址值
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}

 Array as the return value of the method: As the return value of the method, the array is actually the address value of the array.

public class Demo02ArrayReturn {

    public static void main(String[] args) {
        int[] result=calculate(10,20,30);
        System.out.println("mian方法接收到的返回值数组是:"+result);     //地址值
        System.out.println("三个数之和是:"+result[0]);
        System.out.println("三个数的平均数是:"+result[1]);
    }
    public static int[] calculate(int a,int b,int c){
        int sum=a+b+c;
        int avg=sum/3;
        int[] array={sum,avg};
    }
}

Arrays are summarized as return values ​​and parameters: a method can have 0, 1, or multiple parameters, but only 0 or 1 return value, not multiple return values. If you want a method to have multiple return values, use an array to receive it. An array can be used as a method parameter or as a return value of a method. The array is used as the method parameter, and the address value of the array is passed in. As the return value of the method, the array is actually the address value of the array.

The knowledge of the array will be explained here first, if any understanding is not in place, welcome all bloggers to step on the sofa in the comment area!

Guess you like

Origin blog.csdn.net/wtt15100/article/details/108042393