当前位置:网站首页>2022/07/14 learning notes (day07) array

2022/07/14 learning notes (day07) array

2022-07-19 06:05:00 Radical cucumber

Catalog

One . Array concept :

         1.1 Container concept :

         1.2 Array concept :

         1.3 Definition of array :

         1.4 Array access :

         1.5 Array common operations :

Two . Array exercise : 

         1. demand : Find the maximum value from all elements of the array :

         2. demand : Output a by “*” Composed of 5*5 The square of :

                  3. Clean Calculator Beta :

         4. Take a guess :

3、 ... and . Two dimensional array :

        3.1 grammar :


Be humble , learn , Walk forward , Look up

One . Array concept :

        1.1 Container concept :

                Wardrobe : All clothes are stored in one container

        1.2 Array concept :

                An array is a container for storing data with a fixed length , Ensure that the data types of multiple data are consistent

        1.3 Definition of array :

                        Mode one :

Format :

         The array stores the data type of the element [ ]      Array name =new      The data type of the data storage element [ length ]

                        int  [  ] arr = new   int [ 3 ]

                        Array is fixed length   length [3]

                         Array name ——> Variable name Manipulate arrays by array names

                         Mode two : 

Format :

          The array stores the data type of the element [ ]    Array name =new    The data type of the data storage element   [  ]{ Elements 1, Elements 2, Elements 3}

                        int [ ] arr = new int [ ]{10,20,30}

                        Mode three :

Format :

         The array stores the data type of the element [ ]    Array name = { Elements 1, Elements 2, Elements 3}

                        int [ ] arr = {10,20,30}

         1.4 Array access :

        

  Indexes : Every element stored in the array , Will automatically have a number , from 0 Start , This automatic encoding is called array indexing (index), You can index by array (index, Subscript ) Access the elements in the array

Format : Array name [ Indexes ]

                String [ ] strs={" Zhang San "," Li Si "," Wang Wu "};

                System.out.println(strs [ 0 ])

                System.out.println(strs [ 1 ])

                System.out.println(strs [ 2 ])

length : length

                System.out.println(strs .length)

assignment : 

                strs [ 1 ] = " Zhao Liu ";

  characteristic :

                 The length of the array cannot be changed during the running phase of the program

                 The data in the array , The data types must be unified

                Directly print the array name to get the hash value of the address

Use advice :

                1. If you are not sure about the contents of the array, it is recommended to use dynamic initialization

                2. If you determine the contents of the array, it is recommended to use static initialization

        1.5 Array common operations :

                1.5.1 Array traversal :

                //for

                        for ( int i = 0; i < strs.length; i++) {

                                System.out.println(strs[i]);

                                }

                1.5.2 Array subscript out of bounds :

                 There is no index in the access array , The program will throw an out of bounds exception ArrayIndexOutOfBoundsException

                 System.out.println(strs[3]);

                 1.5.3 Arrays are prone to null pointer exceptions :

                         String[] strs={" Zhang San "," Li Si "," Wang Wu "};

                                strs=null;

                                System.out.println(strs[0]);

                                Exception in thread "main" java.lang.NullPointerException

Two . Array exercise : 

       1. demand : Find the maximum value from all elements of the array :

package Array;

public class Array02 {

    public static void main(String[] args) {
        int[] arr={5,15,2000,10000,4000,1500};
        int max=arr[0];//0- value 》
        for (int i = 0; i < arr.length; i++) {
            // If you traverse to an element greater than max
            if(arr[i]>max){
            //max Remember the maximum value 
            max=arr[i];
            }
          }
    System.out.println(" The maximum value in the array :"+max);
  }
}

         2. demand : Output a by “*” Composed of 5*5 The square of :

public class ch04 {
    public static void main(String[] args) {
        /*
         *  Square 
         *           *****
         *           *****
         *           *****
         *           *****
         *           *****/
        for (int i =0 ; i <5; i++) {
            for (int j=0;j<5;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

        3. Clean Calculator Beta :

        demand :                

         Enter two numbers and a symbol from the keyboard 
         Add, subtract, multiply and divide 
         Print out calculation results 
         The main thing is :
        1. The divisor cannot be zero 0
        2. What if the symbol input is wrong ?
        3. Can the accelerator keep counting ?
import java.util.Scanner;

public class jisuqi {
    public static void main(String[] args) {
        h2:// Start from scratch 
        for (; ; ) {
            Scanner src = new Scanner(System.in);
            System.out.println(" Please enter the first number :");
            double num1 = src.nextDouble();// Enter the first number 
            h1:// Start with the second number 
            for (; ; ) {
                System.out.println(" Please enter the second number :");// Enter the second number 
                double num2 = src.nextDouble();
                System.out.println(" Please enter the operation symbol :");
                String sep = src.next();
                switch (sep) {
                    case "*":
                        System.out.println(num1 + sep + num2 + "=" + (num1 * num2));
                        break;
                    case "+":
                        System.out.println(num1 + sep + num2 + "=" + (num1 + num2));
                        break;
                    case "-":
                        System.out.println(num1 + sep + num2 + "=" + (num1 - num2));
                        break;
                    case "/":
                        if (num2 == 0) {
                            System.out.println(" Your input is wrong , The divisor cannot be zero 0, Please re-enter the second number :");
                            continue h1;// Jump to the second number 
                        } else {
                            System.out.println(num1 + sep + num2 + "=" + (num1 / num2));
                            break ;
                        }
                }
                h3:for (;;){
                    System.out.println(" Do you want to continue calculating ? 1. yes   2. no ( Please enter the relevant serial number )");
                    int zxc=src.nextInt();// Fill in the numbers 
                    if (zxc==1){
                        continue h2;// Jump to the head 
                    }else if (zxc==2){
                        System.out.println(" Thank you for using !");
                        break h2;// Jump out of all 
                    }else {
                        System.out.println(" Your input is wrong , Please re-enter ");
                        continue h3;// Exclude other situations 
                    }
                }
            }
        }
    }
}

         4. Take a guess :

import java.util.Scanner;

public class guess {
    public static void main(String[] args) {
        /*
        *  Guess number games 
        *  Enter a number on the keyboard and save it with a variable 
        *  Guess what you entered 
        * */
        /*
        *  Please enter a number 
        * 20
        *  Number saved successfully , Guess the number */
        Scanner num = new Scanner(System.in);
        System.out.println(" Please enter a number :");
        int num1 = num.nextInt();
        System.out.println(" Number saved successfully , Please guess the number ?");
        index :for (;;) {
            int num2 = num.nextInt();
            if (num2 > num1) {
                System.out.println(" The number you entered is larger , Please re-enter ");
                continue index;
            } else if (num2 < num1) {
                System.out.println(" The number you entered is smaller , Please re-enter ");
                continue index;
            } else {
                System.out.println(" congratulations , Your input is correct ");
                break index;

            }
        }
    }
}

3、 ... and . Two dimensional array :

        3.1 grammar :

int [  ] arr = new int [ 3 ] [ 2 ]

1. Statement     Two dimensional array name   arr

2.[ 3 ]  representative     Three one-dimensional arrays -->3 That's ok

3.[ 2 ]  representative   2 It's worth    --> Column

                 Column                  Column
                 That's ok arr [ 0 ] [ 0 ]arr [ 0 ] [ 1 ]
                 That's ok arr [ 1 ] [ 0 ]arr [ 1 ] [ 1 ]

         3.2 example :

        demand : The company's annual sales sum

         The statistical data of a company by quarter and month are as follows : Company ( Ten thousand yuan )

                 first quarter :22,66,44

                 The second quarter :77,33,88

                 The third quarter :25,45,65

                 In the fourth quarter :11,68,99

                seek : Total sales :

package demo02;

public class TwoArray02 {

    public static void main(String[] args) {
        int[][] arrs={
   {22,66,44},{77,33,88},{24,45,65},{11,66,99}};
        int sum=0;
        for(int i=0;i<arrs.length;i++){// Get each one-dimensional array 
            for(int j=0;j<arrs[i].length;j++){// Get every element of every one-dimensional array 
                sum+=arrs[i][j];
            }
         }
        System.out.println(" The sales volume of this year is "+sum);
    }
}

 

原网站

版权声明
本文为[Radical cucumber]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170509284930.html