"Moving Bricks" Android Primer (4)-Java Development Programming Basics-arrays

Source: Internet
Author: User
Tags array definition

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.
      • 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];

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

05.03_java Language Basics (memory allocations 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

05.04_java Language Basics (array of memory plots 11 arrays) (master)

    • A: Drawing Demo
      • An array

05.05_java Language Basics (array of memory plots 22 arrays) (Learn)

    • A: Drawing Demo
      • Two different arrays

05.06_java Language Basics (array of memory plots 33 references two arrays) (learn)

    • A: Drawing Demo
      • Three references, two array references to the same address

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

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.
        • Int[] arr = {A-i};
        • arr = null;
        • System.out.println (Arr[0]);

05.09_java Language Basics (Array operation 1 times calendar) (master)

    • 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;
        1. public static void print (int[] arr) {
        2. for (int i = 0;i < Arr.length; i++) {
        3. System.out.print (Arr[i] + "");
        4. }
        5. }
        Copy Code

05.10_java Language Basics (Array operations 2 get the maximum) (master)

  • a: Case demo
    • array gets the maximum value ( Gets the maximum minimum value in the array)
      1. public static int Getmax (int[] arr) {
      2.     int max = arr[0];
      3.     for (int i = 1;i < arr.length; i++) {         //from the second element of the array, start traversing the /li>
      4.         if (Max < arr[i]) {           &nb sp;            //If Max records a value less than the element in the array
      5.             max = arr[i];                    &nb sp;      //max record larger
      6.         }
      7.    }
      8.     return max;
      9. }
      copy code

05.11_java Language Foundation (operation of Array 3 inversion) (master)

    • A: Case Demo
      • array element inversion (that is, swapping elements)
        1. public static void Reversearray (int[] arr) {
        2. for (int i = 0;i < ARR.LENGTH/2; i++) {
        3. Arr[0] and arr[arr.length-1-0] Exchange
        4. ARR[1] and Arr[arr.length-1-1] Exchange
        5. ARR[2] and Arr[arr.lentth-1-2]
        6. //...
        7. int temp = Arr[i];
        8. Arr[i] = arr[arr.length-1-i];
        9. Arr[arr.length-1-i] = temp;
        10. }
        11. }
        Copy Code

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)
        1. public static char Getweek (int week) {
        2. Char[] arr = {', ' one ', ' two ', ' three ', ' four ', ' V ', ' VI ', ' Day '}; Define a weekly table
        3. return Arr[week]; Get the elements in a table by index
        4. }
        Copy Code

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)
        1. public static int GetIndex (int[] arr,int value) {
        2. for (int i = 0;i < Arr.length; i++) {//array traversal
        3. if (arr[i] = = value) {//If the elements in the array match the elements found
        4. return i;
        5. }
        6. }
        7. return-1;
        8. }
        Copy Code

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
      1. int x;
      2. int y;
      3. int x, y;
      4. Int[] x;
      5. Int[] y[];
      6. Int[] x,y[]; X is a one-dimensional array, and Y is a two-dimensional array
      Copy Code
  • E: Case Demo
    • Define two-dimensional arrays, output two-dimensional array names, one-dimensional array names, one element

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

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

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

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.
        1. Int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};
        2. for (int i = 0;i < Arr.length; i++) {//get to one-dimensional array in each two-dimensional array
        3. for (int j = 0;j < Arr[i].length; J + +) {//Gets the elements in each one-dimensional array
        4. System.out.print (Arr[i][j] + "");
        5. }
        6. System.out.println ();
        7. }
        Copy Code

05.19_java Language Basics (two-dimensional array exercises 2 summation) (master)

  • A: Case Demo
    1. Requirements: Sum of annual sales of company
    2. the data for a company by quarter and month are as follows: unit (million)
    3. first quarter: 22,66,44
    4. second quarter: 77,33,88
    5. third quarter: 25,45,65
    6. fourth quarter: 11,66,99
    7. int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
    8. int sum = 0;                     &nb Sp              //define variables to record the result of each addition
    9. for (int i = 0;i < arr.length; i+ +) {         //get each one-dimensional array
    10.     for (int j = 0;j < Arr[i].length; J + + ) {   //Gets the elements in each one-dimensional array
    11.         sum = sum + arr[i][j];     ;              //cumulative
    12.    }
    13. }
    14. System.out.println (sum);
    Copy Code

05.20_java Language Basics (Study questions in Java and parametric transfer problems and illustrations) (master)

    • A: Case Demo
      1. Read the program to write the results, and draw the memory diagram to explain
      2. public static void Main (string[] args) {
      3. int a = 10;
      4. int B = 20;
      5. System.out.println ("A:" +a+ ", B:" +b);
      6. Change (A, b);
      7. System.out.println ("A:" +a+ ", B:" +b);
      8. Int[] arr = {1,2,3,4,5};
      9. Change (arr);
      10. System.out.println (arr[1]);
      11. }
      12. public static void Change (int a,int b) {
      13. System.out.println ("A:" +a+ ", B:" +b);
      14. A = b;
      15. b = A + B;
      16. System.out.println ("A:" +a+ ", B:" +b);
      17. }
      18. public static void Change (int[] arr) {
      19. for (int x=0; x<arr.length; x + +) {
      20. if (arr[x]%2==0) {
      21. arr[x]*=2;
      22. }
      23. }
      24. }
      Copy Code

"Moving Bricks" Android Primer (4)-Java Development Programming Basics-arrays

Related Article

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.