Overview of arrays

1, Array overview

  • These data sets are uniformly numbered by array and managed by a certain order.
  • Common concepts of array: array name, subscript (or index), element and length of array
  • The characteristic of array is that the array is arranged in order
  • The array itself is a reference data type, and the elements in the array can be any data type, including basic data type and reference data type.
  • Creating an array object will open up a whole contiguous space in memory, and the first address of this contiguous space is referenced in the array name
  • Once the length of the array is determined, it cannot be modified.
  • We can directly call the element at the specified position by subscript (or index), which is very fast
  • Classification of arrays:
    • By dimension: one dimensional array, two-dimensional array, three-dimensional array
    • According to the data type of the element, it is divided into: array of basic data type elements and array of reference data type elements (i.e. object array)

2, Use of one-dimensional array

2, Use of one-dimensional array
1. Declaration and initialization of one-dimensional array.
2. How to call the element at the specified position of the array
3. How to get the length of the array
4. How to traverse an array
5. Default initialization value of array element
>Array element is integer: 0
>Array elements are floating point: 0.0
>Array elements are char type: 0(ASCII code) or '\ u0000', not '0'
>Array elements are boolean: false

> when the array element is a reference data type: null (null value), not "null"

‚Äč 6. Memory parsing of array


Stack on the left and heap on the right.

Occupy a continuous space in the heap, save the first memory address of the space in hexadecimal form and assign it to the array name in the stack (only the first memory address of the array is saved in the stack)

practice

Exercise 1:
Rental housing: shengjingfang single room is rented for a short period of four months, 550 / month (water, electricity and coal pool, network fee 35 / month). Air conditioning, toilet and kitchen are complete. The house is full of people from the IT industry.

public class Prictice {
    public static void main(String[] args) {
		public static void tel(){
            int[] arr = new int[]{8,2,1,0,3};
            int[] index = new int[]{2,0,3,2,4,0,1,3,2,3,3};
            String tel = "";
            for (int i = 0; i < index.length;i++){
                tel += arr[index[i]];
            }
            System.out.println("contact information:" + tel);
        }
    }
}

Exercise 2:

public class Prictice {
    public static void main(String[] args) {
		public static void cj(){
            //1. Use Scanner to read the number of students
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter the number of students:");
            int number = scan.nextInt();

            //2. Create an array, store student scores and initialize dynamically
            int[] scoers = new int[number];
            //3. Traverse the array and assign values to the elements in the array
            System.out.println("Please enter" + number + "Student grades");
            int maxScore = 0;
            for(int i = 0; i < scoers.length;i++){
                scoers[i] = scan.nextInt();
                    if(maxScore < scoers[i]){
                       maxScore = scoers[i];
                    }
            }
            //4. Get the maximum value of the elements in the array: the highest score
//          int maxScore = 0;
//        	for (int i = 0; i < scoers.length;i++){
//           	 if(maxScore < scoers[i]){
//            	    maxScore = scoers[i];
//          	  }
//     	   }
            //5. According to the difference between each student and the highest score, get the grade of each student, and output the grade and score
            char level;
            for (int i = 0; i < scoers.length;i++){
                if (maxScore - scoers[i] <= 10){
                    level = 'A';
                }else if (maxScore -scoers[i] <= 20){
                    level = 'B';
                }else if (maxScore -scoers[i] <= 30){
                    level = 'C';
                }else{
                    level = 'D';
                }

                System.out.println("student " + i + " score is "
                + scoers[i] + ",grade is " + level);
            }

        }
    }
}

3, Use of multidimensional arrays

  • The syntax of multidimensional array is provided in java language
  • If a one-dimensional array can be regarded as a linear figure in geometry, then a two-dimensional array is equivalent to a table, just like a table in Excel

  • For the understanding of two-dimensional array, we can see that one-dimensional array array1 exists as an element of another one-dimensional array array2. In fact, from the perspective of the underlying operation mechanism of the array, there is no multidimensional array

    /*
    Use of two-dimensional arrays
        Regulation: the two-dimensional array is divided into the elements of the outer array and the elements of the inner array
            int[] arr = new int[4][3];
            Outer elements: arr[0],arr[1], etc
            Inner elements: arr[0][1],arr[1][2], etc
          1.Understanding: for the understanding of two-dimensional array, we can see that one-dimensional array array1 exists as an element of another one-dimensional array array2. In fact, from the perspective of the underlying operation mechanism of the array, there is no multidimensional array
          2.1.Declaration and initialization of two-dimensional array.
          2.2.How to call an element at a specified position of an array
          2.3.How to get the length of an array
          2.4.How to traverse an array
          2.5.Default initialization value of array elements
          For initialization method 1: for example: int[][] arr = new int[4][3];
                    The initialization value of the outer element is: address value
                    The initialization value of the inner element is: the same as that of the one-dimensional array
          For initialization method 1: for example: int[][] arr = new int[4] [];
                    The initialization value of the outer element is null
                    The initialization value of the inner element is: cannot be called (null pointer exception will be reported)
          2.6.Memory parsing of array
     */
    
  • Memory parsing of two-dimensional array


Tags: Java

Posted by Lateuk on Thu, 12 May 2022 20:13:08 +0300