The Java self-study road-DAY04

Source: Internet
Author: User
Tags array definition

Java04switch statements
    1. Format:

Switch () {

Case: Constant 1

EXECUTE statement

Break

Default

EXECUTE statement

Break

}

    1. The case satisfies the execution case execution statement, jumps out of the loop, otherwise executes the default execution statement;
    2. Case: Output Day of the week
    3. Switch expression type

L JDK 0-1.4 byte short int char

L jdk1.5 byte short int char enum

L jdk1.7 byte short int char enum String (after Sun Company acquired)

    1. Case penetrating

Case penetration refers to a switch statement that executes from the matching case label based on the value of the expression and executes until the break statement or at the end of the switch statement. If there is no break statement, then it starts at the case label that matches the value of the expression and executes to the end of the switch statement, starting at one of the case, ignoring the matching of subsequent values, and the phenomenon of running the contents of the case directly is the phenomenon of penetrating. No break will always penetrate execution.

Switch () {

CASE1:

CASE2:

CASE3:

CASE4:

EXECUTE statement

Break

}

Array
    1. A container used to store data
    2. Data is stored in an array
    3. Defining rules

L Data type

L Container Size

L Formula: Data type [] variable name =new data type [number of stored elements];

L Data type: The data type of the storage element

l [] Expression array

L New Create a container keyword

l [] represents an array

L Number: Is the number of data stored

L constant array size, cannot be changed once determined

L Reference type

L Memory Issues

    1. Operating System management hardware (memory)
    2. JVM Search System
    3. The operating system is allocated a portion of the JVM's memory, managed by the JVM
    4. The JVM divides its memory into five regions.

A) Register: between memory and CPU (not our concern)

b) Local method stack: a function that the JVM does not perform, and the JVM invokes the functionality in the system (currently irrelevant)

c) Sharing of methods and data: the memory portion of the run-time class file (related to development)

d) method Stack: When all the methods run, enter the memory portion

e) heap: stored Containers and objects

Java open up memory space do not shut down

C + + Shutdown Memory space requires manual shutdown

One-dimensional arrays

L Array Memory issues

Array Memory Graph:

public class arraydemo{

Main () {

Int[] Arr=new int[3];

}

}

A) First the method area to run the Arraydemo.class file

b) Then main enters the method stack run, i.e. the stack runs (advanced at the bottom), the method stack opens up a part to run the Main method

c) Then create a new array container the JVM will open up a portion of the memory inside the storage array container and according to the memory size of the average, each data in memory memory address, (auto-numbering, the minimum is 0, the maximum storage length-1, automatic numbering is index, subscript, corner superscript); The first address of the array is the first data address , the address of each data is continuous;

d) The virtual machine then returns the address to the defined variable arr, which stores the variable arr, which points to the memory address;

e) Print arr: print multiple times differently because memory is randomly allocated

f) Print finished the main method ends, the ARR variable fails, and the allocation space inside the heap fails

G

L Array Access formula

    1. Each data in memory has memory address, (auto number, the minimum value is 0, the maximum storage length-1, automatic numbering is index, subscript, corner superscript);
    2. Variable name [subscript];

L Length Property

    1. Maximum index Length-1;

L Array Definition method

    1. Assign a value to an element at the same time as defined

A) data type [] variable name =new data type [] {};

Int [] arr=new inr[]{1,2,3,4,5};

b) do not allow length to be defined in brackets, otherwise compilation fails

c) Avoid the risk of logic errors and space waste resulting from the subsequent elements saying more than or less than the defined space;

    1. No new keyword

A) data type [] variable name ={};

b) Convenient and simple, recommended use;

L Assign a value to an element

Int [] arr=new int[5];

arr[1]=3;

L Summary

Int[] Arr=new int[5];

arr[1]=2;

Int[] Arr=new int[]{2};

Int[] Arr={2};

L Traversal Array

    1. For loop implementation

A) for loop has been cycle times, counter thought

b) while unknown loop at this time

    1. for (i=0;i<arr.length-1;i++) {

Output I

}

L exception

    1. Out-of-bounds exceptions (negative index and hyper-order access)

A) successful compilation

b) Run error (only the runtime will use the memory load method and perform the operation inside, see memory problems above)

    1. Null pointer exception

A) normal phenomenon

b) Non-normal phenomenon

The assignment array element is nul,null as a special constant, the reference type can be null, at which point the variable does not save this element address or does not point to this element address, at this time again access that element will be reported null pointer exception, the virtual machine heap cleanup This element memory, cut cannot be reassigned address;

L Array Maximum value thought

    1. Int[] arr={1,5,8,9,3,}; (not before, can be one more comma,)

Int Max=arr[0];

    1. for (i=1;i<arr.length-1;i++) {

If (Max<arr[i])

{

Max=arr[i];

}

}

Output Max

Two-dimensional array (stores multiple one-dimensional arrays)

L Define the way

    1. Int [] [] arr=new int[3] [4];
    2. One more []
    3. Representation: In a two-dimensional array, there are three one-dimensional arrays, three one-dimensional arrays, and the length of each array is 4

L Memory diagram

    1. When it is created, it first allocates a memory space in memory to a two-dimensional array, divided into three parts
    2. Each one-dimensional array is then allocated one memory space, and each one-dimensional array is divided into four parts; each one-dimensional array has a first address,
    3. A two-dimensional array contains the first address of each one-dimensional array
    4. The two-dimensional array variable points to the first address of a two-dimensional array, the address of the first one-dimensional array;

    1. Defining special cases with two-dimensional arrays

L int[] [] arr=new Int [3][];

L cannot assign a two-dimensional array arr[0]=new int [5] at this time.

    1. A simple way to define

a) Int [] [] arr={{1,4},{5,6},{7,8,9}};

b) allow one-dimensional array lengths to vary

L Traverse

    1. Two for loops to traverse
    2. Outer loop loops through two-bit arrays
    3. Memory loop traversal one-dimensional array
    4. for (int i=0;i<arr.length;i++) {

for (int j=0;j<arr[i].length;j++) {

Output ARR[I][J];

}

}

Exercise: Random Name Register

    1. Function:

A) store the name of the class

b) Overview of the class name

c) A person's name is randomly given

    1. Comment Amount up to 30% of source code

comments in the Java language

Program comments are an important part of the source code, for a specification of the program source code, the note should be accounted for more than 1/3 of the source code. Almost all programming languages provide a way to annotate, the General programming language provides basic single-line comments and multiline comments, the Java language is no exception, in addition, the Java language provides a document annotation. There are three types of comments in the Java language

Single-line Comment

Multi-line comments

Document comments

    1. 3. re

Core Vocabulary

English [re?] Beauty [re?]

Prep. About

Pref. Again, re-

N. The second tone of the long scale

    1. Implementation of the selection function in the method

L If the user enters the function selection input, other than the specified characters, you can use the Else statement to re-execute the method, the implementation of the function is perfect, if the method is only as the interface display, otherwise can not be re-executed;

04.zip

The Java self-study road-DAY04

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.