Java EE Basics (v)

Source: Internet
Author: User
Tags array definition array length

1. Java Language Foundation (array overview and Definition Format description)
    • 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.
      • 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];

2. Java Language Foundation (initialization of arrays initialized dynamically)
    • 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
3. Java Language Foundation (memory allocation in Java and differences between stacks and heaps)
    • A: Stack (Master)
      • Store local Variables
    • B: Heap (Master)
      • Storing new arrays or objects
    • C: Method Area
      • Object-oriented section explanation
    • D: Local Method area
      • and system-related
    • E: Register
      • For CPU use
4. Java Language Foundation (array of memory plots 11 arrays)
    • A: Drawing Demo
      • An array
5. Java Language Foundation (array of memory plots 22 arrays)
    • A: Drawing Demo
      • Two different arrays
6. Java Language Foundation (array of memory plots 33 references two arrays)
    • A: Drawing Demo
      • Three references, two array references to the same address
7, Java Language Foundation (initialization of the array static initialization and memory diagram)
    • 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
8. Java Language Basics (two common minor problems with array operations out of bounds and null pointers)
    • 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.
        • Int[] arr = {A-i};
        • arr = null;
        • System.out.println (Arr[0]);
9, the Java Language Foundation (array operation 1 times calendar)
    • A: Case Demo

      • Array traversal: Is the output of each element in the array sequentially.
      • Properties of the array: length of the arr.length array
      • Maximum index of the array: arr.length-1;

        public static void print(int[] arr) {    for (int i = 0;i < arr.length ;i++ ) {        System.out.print(arr[i] + " ");    }}
10, the Java Language Foundation (array of Operations 2 get the most value)
    • A: Case Demo

      • Array gets the maximum value (gets the minimum value in the array)

        public static int getMax(int[] arr) {    int max = arr[0];    for (int i = 1;i < arr.length ;i++ ) {          //从数组的第二个元素开始遍历        if (max < arr[i]) {                         //如果max记录的值小于的数组中的元素            max = arr[i];                           //max记录住较大的        }    }    return max;}
11. Java Language Foundation (operation of Array 3 inversion)
    • A: Case Demo

      • array element inversion (that is, swapping elements)

        public static void reverseArray(int[] arr) {    for (int i = 0;i < arr.length / 2 ; i++) {        //arr[0]和arr[arr.length-1-0]交换        //arr[1]和arr[arr.length-1-1]交换        //arr[2]和arr[arr.lentth-1-2]        //...        int temp = arr[i];        arr[i] = arr[arr.length-1-i];        arr[arr.length-1-i] = temp;    }}
12, the Java Language Foundation (array of Operations 4 look up the table method)
    • A: Case Demo

      • Array Lookup Table method (according to the keyboard input index, find the corresponding week)

        public static char getWeek(int week) {    char[] arr = {‘ ‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘,‘日‘};     //定义了一张星期表    return arr[week];                                           //通过索引获取表中的元素}
13, the Java Language Foundation (array of Operations 5 basic lookup)
    • A: Case Demo

      • Array element Lookup (finds the index of the first occurrence of the specified element in the array)

        public static int getIndex(int[] arr,int value) {    for (int i = 0;i < arr.length ;i++ ) {              //数组的遍历        if (arr[i] == value) {                          //如果数组中的元素与查找的元素匹配            return i;        }    }    
14, the Java Language Foundation (two-dimensional array overview and Format 1 explanation)
    • 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
15, Java Language Foundation (two-dimensional array format 1 memory plot)
    • A: Drawing Demo
      • Drawing explains the problem of the above two-dimensional array name, one-dimensional array name, and the value of an element
16, Java Language Foundation (two-dimensional array format 2 explanation and memory plot)
    • 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
17, Java Language Foundation (two-dimensional array format 3 explanation and memory plot)
    • 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
18, Java Language Foundation (two-dimensional array exercises 1 times calendar)
    • 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.

        int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};for (int i = 0;i < arr.length ;i++ ) {          //获取到每个二维数组中的一维数组    for (int j = 0;j < arr[i].length ;j++ ) {   //获取每个一维数组中的元素        System.out.print(arr[i][j] + " ");    }    System.out.println();}
19. Java Language Basics (two-dimensional array exercises 2 summation)
    • A: Case Demo
    • 需求:公司年销售额求和某公司按照季度和月份统计的数据如下:单位(万元)第一季度:22,66,44第二季度:77,33,88第三季度:25,45,65第四季度:11,66,99int[][] 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);
20, the Java Language Foundation (study questions Java in the parameter transfer problem and diagram)
    • A: Case Demo
    • 看程序写结果,并画内存图解释public static void main(String[] args) {    int a = 10;    int b = 20;    System.out.println("a:"+a+",b:"+b);    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) {    System.out.println("a:"+a+",b:"+b);    a = b;    b = a + b;    System.out.println("a:"+a+",b:"+b);}public static void change(int[] arr) {    for(int x=0; x<arr.length; x++) {        if(arr[x]%2==0) {            arr[x]*=2;        }    }}

Java EE Basics (v)

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.