05 array Overview and Definition Format description

Source: Internet
Author: User
Tags array definition

Class Testx {
public static void Main (string[] args) {
/* int []arr={1,2,3};
int []arr1=new int [2]; /This is an array definition of two methods, static can be assigned value, dynamic cannot be defined when the assignment,
/

int[][]a={{1,2},{2,3,}};
int [][]a1=new int[2][2];
int [][]a2=new int [2][];//dynamic definition must be new with new, no static need to use new,
a2[0]=new int [2];
A2[1]=new Int[3];
*/This is a two-dimensional array definition of three ways, in which only static mode can be all assigned, dynamic can only have initialization values;
System.out.println (arr[1]+ "" +arr1[0]+ "" +a[1]+ "" +a+ "" +a[1][1]+ "" +a1[1][1]+ "" +a2[1][1] ");
}
}

05.01_java Language Basics (Array overview and Definition Format description) (learn)
    • A: Why do we have arrays (containers)
      • To store multiple values of the same data type
    • B: Array Concept

      • An array is a collection of multiple elements that store the same data type. can also be seen as a container.
        In memory is continuous, query fast modification Delete slow,
      • Arrays can store either the base data type or the reference data type.
    • C: Array definition Format
      data type [] Array name = new data type [length of array];
      Case:
      Class Demo1_array {
      public static void Main (string[] args) {
      int x = 10;
      x = 20;
      System.out.println ("x =" + x);
      data type [] Array name = new data type [length of array];
      int[] arr = new INT[5]; Can store five data of type int
      /*
      Left:
      int: Data type
      []: The array represented, several square brackets represent a few-dimensional array
      ARR: Legal identifiers
      Right:
      NEW: Create a fresh entity or object
      int: Data type
      []: the array represented
      5: Represents the length of the array
      */

      }
      }

05.02_java Language Basics (initializing dynamic initialization of arrays) (mastering)
  • A: What is the initialization of an array
    • is to open up contiguous memory space for arrays and assign values to each array element
  • B: How to initialize an array
    • A: Dynamic initialization only specifies the length, the system gives the initialization value
      • int[] arr = new INT[5];
    • B: Static initialization gives the initialization value, the system determines the length of the
  • C: Dynamic initialization format:
    • data type [] Array name = new data type [array length];
  • D: Case Demo

    • Output array names and arrays elements

      /*
      Integer type: Byte,short,int,long default initialization value is 0
      Floating-point type: float,double default initialization value is 0.0
      Boolean type: Boolean default initialization value false
      Character type: Char default initialization value ' \u0000 ' preceded by ' ', only one character is allowed, but ' \ ' represents the escape character, U represents Unicode encoding, Java uses Unicode encoding, \u is equivalent to
      The back of the 4 0 was escaped.
      Char occupies two bytes in memory and is 16 bits
      \u0000, each of the 0 actually represents the 16 binary 0, then four 0 is representative of 16 bits

    [[Email protected]

    [The representation is an array, and a few represent several dimensions.
    I represents the int type
    @ is fixed
    19BB25A represents the address value of the array
    */
    Class Demo2_array {
    public static void Main (string[] args) {
    data type [] Array name = new data type [array length];
    int[] arr = new INT[5]; Dynamic initialization, opening up contiguous 5 blocks of space in memory

    System.out.println(arr[0]);         //系统给出默认初始化值,整数类型的都是0arr[0] = 10;System.out.println(arr[0]); System.out.println(arr);            //[[email protected]

    }
    }

05.03_java Language Basics (memory allocations in Java and differences between stacks and heaps)
    • A: Stack (Master)
      • Storage of local variables, contiguous allocation of memory, advanced post-out, bullet shells
    • B: Heap (Master)
      • The array or object that stores the new is not contiguous. The array elements are contiguous,
    • C: Method Area
      • Object-oriented section explanation
    • D: Local Method area
      • and system-related
    • E: Register
      • For CPU use
        When compiling the class file will be generated, these class files will be saved on the hard disk, when the runtime, the class file is loaded into memory, in memory to carry out code execution, there is also into the stack, who into the stack, who into the heap, to see where it belongs, if it is a local variable into the stack, New things come out into the heap. Be sure to run into memory to execute, and if you open too many apps in your computer, your computer will be stuck.

        Case:
        Class Demo3_array {
        public static void Main (string[] args) {
        int[] arr = new INT[3];
        }
05.04_java Language Basics (array of memory plots 11 arrays) (master)
    • A: Drawing Demo

      • An array
        Case:
        Class Demo3_array {
        public static void Main (string[] args) {
        int[] arr = new INT[3]; Dynamic initialization, creating 3 contiguous blocks of space
        System.out.println (arr);
        Arr[0] = 10;
        ARR[1] = 20;

        System.out.println (Arr[0]);
        System.out.println (arr[1]);
        }
        }

05.05_java Language Basics (array of memory plots 22 arrays) (Learn)
    • A: Drawing Demo

      • Two different arrays
        Case:
        Class Demo4_array {
        public static void Main (string[] args) {
        int[] arr1 = new Int[3]; Create an array with a length of 3
        int[] arr2 = new Int[3]; Create an array with a length of 3

        System.out.println (ARR1); Print the address value of an array
        System.out.println (ARR2);

        Arr1[0] = 10; Assigns a value to the first element in the first array
        ARR2[1] = 20; Assigns a value to the second element in the second array

        System.out.println (Arr1[0]);
        System.out.println (arr1[1]);
        System.out.println (arr1[2]);

        System.out.println ("———————————— –");

        System.out.println (Arr2[0]);
        System.out.println (arr2[1]);
        System.out.println (arr2[2]);
        }
        }

05.06_java Language Basics (array of memory plots 33 references two arrays) (learn)
    • A: Paint demo

      • Three references, two array references to the same address
        Case:
        //Three references two arrays
        class Demo5_array {
        Public st atic void Main (string[] args) {
        int[] arr1 = new Int[3];
        int[] arr2 = new INT[5];
        int[] Arr3 = arr2;

        System.out.println (ARR1);
        System.out.println (ARR2);
        System.out.println (ARR3);

        Arr1[0] = 10;
        Arr1[1] =;

        Arr2[1] = 30;
        Arr3[1] = 40;
        Arr3[2] =;

        System.out.println (arr1[0]);
        System.out.println (arr1[1]);
        System.out.println (arr1[2]);
        System.out.println ("——————————-");
        System.out.println (arr2[0]);
        System.out.println (arr2[1]);
        System.out.println (arr2[2]);
        System.out.println (arr2[3]);
        System.out.println (arr2[4]);
        System.out.println ("——————————-");
        System.out.println (arr3[0]);
        System.out.println (arr3[1]);
        System.out.println (arr3[2]);
        System.out.println (arr3[3]);
        System.out.println (arr3[4]);
        }
        }

05.07_java Language Basics (initializing static initialization of arrays and memory graphs) (master)
    • A: The Static initialization format:
      • Format: data type [] Array name = new data type []{element 1, element 2,...};
      • Simplified format:
        • data type [] Array name = {element 1, element 2,...};
    • B: Case Demo
      • Explanation of the logarithm group
      • Output array names and arrays elements
    • C: Drawing Demo

      • An array
        Case:
        Class Demo6_array {
        public static void Main (string[] args) {
        data type [] Array name = new data type []{element 1, element 2,...};
        int[] arr = new int[5]{11,22,33,44,55}; This is wrong, do not allow the combination of movement
        Int[] arr2 = {11,22,33,44,55}; Shorthand form for static initialization

        Int[] arr; Declaring an array reference
        arr = new int[]{11,22,33,44,55};

        Int[] ARR2;
        ARR2 = {11,22,33,44,55}; Short form Declaration and assignment on the same line, so write will error

        System.out.println (ARR2);
        System.out.println (Arr2[4]);
        }
        }

05.08_java Language Basics (two common minor problems with array manipulation out of bounds and null pointers) (master)
    • A: Case Demo

      • A:arrayindexoutofboundsexception: Array index out-of-bounds exception
        • Reason: You have accessed an index that does not exist.
      • B:nullpointerexception: null pointer exception

        • Cause: The array is no longer pointing to heap memory. You also use the array name to access the element.
          Case:
          Class Demo7_exception {
          public static void Main (string[] args) {
          int[] arr = new INT[5]; 0x0011
          System.out.println (Arr[-1]); Index out-of-bounds exception occurs when accessing an index that does not exist in the array

        arr = null;
        System.out.println (Arr[0]); Null pointer exception occurs when an array reference is assigned to NULL, and then the element in the array is called
        Must be initialized before local variables are used
        }
        }

05.09_java Language Basics (Array operation 1 times calendar) (master)
  • A: Case demo

    • Array Traversal: This is the output of each element in the array sequentially. The properties of the
    • array: The length of the Arr.length array string.length (); size (); The maximum index of the
    • Array: arr.length-1;
      Case:
      class Demo8_array {
      public static void Main (string[] args) {
      int[] arr = {11,22,33,44,55};

      / System.out.println (arr[0]);//The reusability of this code is too poor, we learn the Loop
      System.out.println (arr[1]);
      System.out.println (arr[2]);
      System.out.println (arr[3]);
      System.out.println (arr[4]);
      /

      for (int i = 0;i < 5; i++) {
      System.out.println (arr[i]);
      }
      System.out.println ("—————"); The
      //arr.length represents the length of the array
      System.out.println (arr.length);
      for (int i = 0;i < Arr.length; i++) {
      System.out.println (arr[i]);
      }

      Int[] arr2 = {3,4,5};
      Print (ARR2);
      }

    /*
    Array traversal
    1, return value type void
    2, parameter list int[] Arr
    */

    public static void print (int[] arr) {
    for (int i = 0;i < Arr.length; i++) {
    System.out.print (Arr[i] + "");
    }
    }
    }

05.10_java Language Basics (Array operations 2 get the maximum) (master)
    • A: Case Demo

      • Array gets the maximum value (gets the minimum value in the array)
        Case:
        Class Demo9_array {
        public static void Main (string[] args) {
        Int[] arr = {33,77,22,44,55};
        int max = Getmax (arr);
        SYSTEM.OUT.PRINTLN (max);
        }

      /*
      Gets the maximum value in the array
      1, return value type int
      2, parameter list int[] Arr
      */

      public static int Getmax (int[] arr) {
      int max = arr[0];
      for (int i = 1;i < Arr.length; i++) {//start traversing from the second element of the array
      if (Max < arr[i]) {//If the value of Max record is less than the element in the array
      max = Arr[i]; Max Records the larger
      }
      }

      return max;

      }
      }

05.11_java Language Foundation (operation of Array 3 inversion) (master)
  • A: Case Demo

      • array element inversion (that is, swapping elements)
        Case:
        Class Demo10_array {
        public static void Main (string[] args) {
        Int[] arr = {11,22,33,44,55};
        Reversearray (arr);
        Print (arr);
        }

    /*
    Array element inversion
    1, explicit return value type void
    2, clear parameter list int[] Arr
    */

    public static void Reversearray (int[] arr) {
    for (int i = 0;i < ARR.LENGTH/2; i++) {
    Arr[0] and arr[arr.length-1-0] Exchange
    ARR[1] and Arr[arr.length-1-1] Exchange
    ARR[2] and Arr[arr.lentth-1-2]
    ...

        int temp = arr[i];    arr[i] = arr[arr.length-1-i];    arr[arr.length-1-i] = temp;}}public static void reverseArray1(int[] arr) {for(int i=0,j=arr.length-1;i<=j;i++,j--) {    int temp = arr[i];    arr[i] = arr[j];    arr[j] = temp;}

    }

    /*
    Array traversal
    1, explicit return value type void
    2, clear parameter list int[] Arr
    */

    public static void print (int[] arr) {
    for (int i = 0;i < Arr.length; i++) {//iterate through each element in the array
    System.out.print (Arr[i] + ""); Print in console
    }
    }
    }

05.12_java Language Basics (Array operations 4 look-up Table method) (master)
    • A: Case Demo

      • Array Lookup Table method (according to the keyboard input index, find the corresponding week)
        Case:
        Import Java.util.Scanner;
        Class Demo11_array {
        public static void Main (string[] args) {
        Scanner sc = new Scanner (system.in);
        System.out.println ("Please enter the corresponding week range at 1-7");
        Int week = Sc.nextint ();

        SYSTEM.OUT.PRINTLN ("Week" + Getweek (week));
        }

      /*
      Returns the corresponding week according to the index
      1, return value type Char
      2, parameter list int week
      */

      public static char Getweek (int week) {
      Char[] arr = {', ' one ', ' two ', ' three ', ' four ', ' V ', ' VI ', ' Day '}; Define a weekly table
      return Arr[week]; Get the elements in a table by index
      }
      }

05.13_java Language Basics (array of Operations 5 basic Find) (master)
    • A: Case Demo

      • Array element Lookup (finds the index of the first occurrence of the specified element in the array)
        Case:
        Class Demo12_array {
        public static void Main (string[] args) {
        Int[] arr = {11,22,33,44,55,66,77};
        int index = GetIndex (arr,88);
        SYSTEM.OUT.PRINTLN (index);
        }

      /*
      Find Element Index
      1, return value type int
      2, clear parameter list, int[] Arr,int value
      */

      public static int GetIndex (int[] arr,int value) {
      for (int i = 0;i < Arr.length; i++) {//array traversal
      if (arr[i] = = value) {//If the elements in the array match the elements found
      return i;
      }

      }return -1;

      }
      }

05.14_java Language Basics (two-dimensional array overview and Format 1) (Learn)
    • A: A Two-dimensional array overview
    • B: two-dimensional array format 1
      • int[][] arr = new int[3][2];
    • C: Interpretation of the two-dimensional array format 1
    • D: Precautions

      • A: The following format can also represent a two-dimensional array
        • 1: Data type array name [] = new data type [m][n];
        • 2: Data type [] array name [] = new data type [m][n];
      • B: Note the differences defined below
      • int x;int y;int x,y;int[] x;int[] y[];int[] x,y[];    x是一维数组,y是二维数组
    • E: Case Demo

      • Define two-dimensional arrays, output two-dimensional array names, one-dimensional array names, one element
        Case:
        Class Demo1_array {
        public static void Main (string[] args) {
        int[][] arr = new int[3][2];
        /*
        This is a two-dimensional array
        There are 3 one-dimensional arrays in this two-dimensional array
        2 elements in each one-dimensional array

        [[[Email protected]//two-D array address value
        [[Email protected]//address value of one-dimensional array
        0//Element value
        */
        System.out.println (arr); Two-dimensional arrays
        System.out.println (Arr[0]); The first one-dimensional array in a two-dimensional array
        System.out.println (Arr[0][0]); The first element of the first one-dimensional array in a two-dimensional array
        }
        }

05.15_java Language Basics (memory plots for two-dimensional array format 1) (Learn)
    • A: Drawing Demo

      • Drawing explains the problem of the above two-dimensional array name, one-dimensional array name, and the value of an element
        Class Demo2_array {
        public static void Main (string[] args) {
        int[][] arr = new int[3][2];

        System.out.println (arr); Print two-dimensional arrays
        System.out.println (Arr[0]); Print the first one-dimensional array in a two-dimensional array
        System.out.println (Arr[0][0]); Prints the first element in the first one-dimensional array in a two-dimensional array
        }
        }

05.16_java Language Basics (Explanation of two-dimensional array format 2 and its memory plots) (learn)
    • A: Two-dimensional array format 2
      • int[][] arr = new int[3][];
    • B: Interpretation of the two-dimensional array format 2
    • C: Case Demo

      • Explain formats, output data, and draw memory graphs
        Case:
        Class Demo3_array {
        public static void Main (string[] args) {
        int[][] arr = new int[3][]; This is a two-dimensional array with three one-dimensional arrays in the two-dimensional array, and three one-dimensional arrays that are not assigned a value

        System.out.println (Arr[0]);
        System.out.println (arr[1]);
        System.out.println (arr[2]);
        Arr[0] = new INT[3]; Three int values can be stored in the first one-dimensional array

         //   arr[1]= new int []{}//不可取

        ARR[1] = new INT[5]; The second one-dimensional array can store five int values
        System.out.println ("——————");
        System.out.println (Arr[0]);
        System.out.println (arr[1]);
        System.out.println (arr[2]);
        }
        }

05.17_java Language Basics (Explanation of two-dimensional array format 3 and its memory plots) (learn)
    • A: Two-dimensional array format 3
      • Int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};
    • B: Interpretation of the two-dimensional array format 3
    • C: Case Demo
      • Explain formats, output data, and draw memory graphs
        Case:
        Class Demo4_array {
        public static void Main (string[] args) {
        Int[][] arr = {{1,2,3},{4,5},{6,7,8,9}}; This is a two-dimensional array in which each brace in a two-dimensional array represents a one-dimensional array
        System.out.println (arr); [[[Email protected], the address value of a two-dimensional array
        System.out.println (Arr[0]); [[Email protected], the address value of a one-dimensional array
        System.out.println (Arr[0][0]); 1, element values in a one-dimensional array
        }
        }
05.18_java Language Basics (two-dimensional array Exercise 1-times Calendar) (master)
    • A: Case Demo

      • Requirements: two-dimensional array traversal

      • The outer loop controls the length of the two-dimensional array, which is actually the number of one-dimensional arrays.

      • The inner loop controls the length of a one-dimensional array.
        Case:
        Class Test1_array {
        public static void Main (string[] args) {
        Int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};

        for (int i = 0;i < Arr.length; i++) {//get to one-dimensional array in each two-dimensional array
        for (int j = 0;j < Arr[i].length; J + +) {//Gets the elements in each one-dimensional array
        System.out.print (Arr[i][j] + "");
        }

        System.out.println();

        }
        }
        }

05.19_java Language Basics (two-dimensional array exercises 2 summation) (master)
    • A: Case Demo
    • 需求:公司年销售额求和某公司按照季度和月份统计的数据如下:单位(万元)第一季度:22,66,44第二季度:77,33,88第三季度:25,45,65第四季度:11,66,99

      Case:
      Class Test2_array {
      public static void Main (string[] args) {
      Int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};

      int sum = 0;                                    //定义变量,记录每次相加的结果for (int i = 0;i < arr.length ;i++ ) {          //获取每一个一维数组    for (int j = 0;j < arr[i].length ;j++ ) {   //获取每一个一维数组中的元素        sum = sum + arr[i][j];                  //累加    }}System.out.println(sum);

      }
      }

05.20_java Language Basics (Study questions in Java and parametric transfer problems and illustrations) (master)
    • A: Case Demo
      Case:
      /*
      The value of the base data type is passed without changing the original value, because the stack is played after the call and the local variable disappears
      The value of the reference data type is passed, changing the original value, because even if the method is on the stack, but the heap memory array object is still in, you can continue to access through the address

Whether it's a value or a pass in Java
1, which is both a pass-through, a pass-through address, a value passed by a basic data type, an address passed by a reference data type
2,java only value, because the address value is also the value (go out to interview all say this, supporters are high commander (father of Java))
/
Class Test3_array {
public static void Main (string[] args) {
/
int a = 10;
int B = 20;
System.out.println ("A:" +a+ ", B:" +b); A = 10,b = 20
Change (A, b);
System.out.println ("A:" +a+ ", B:" +b); //?*/

    int[] arr = {1,2,3,4,5};    change(arr);    System.out.println(arr[1]);}public static void change(int a,int b) {        //a = 10, b= 20    System.out.println("a:"+a+",b:"+b);         //a = 10,b = 20    a = b;                                      //a = 20    b = a + b;                                  //b = 40    System.out.println("a:"+a+",b:"+b);         //a = 20, b = 40}public static void change(int[] arr) {          //1,4,3,8,5    for(int x=0; x<arr.length; x++) {        if(arr[x]%2==0) {            arr[x]*=2;        }    }}

}

05.21_DAY05 Summary
    • Summarize today's knowledge points once again.
      Exercises:
    • Requirements: analog password encryption
      Keyboard input 6 number of int type password
      In the array and then invert,//invert the array
      The inverted array is marked with an odd number of 1 and 3 for 3 and 5 for the other
      Then the last corner of the array is marked with an odd number of elements and the first corner is marked as an odd-numbered element in the group Exchange
      Print it out on the European code is [1,2,3,4,5,6] the answer is [6, 5, 4, 1, 2, 3]


From for notes (Wiz)

05 array Overview and Definition Format description

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.