Chapter 1 of JAVASE, Chapter 3, program control and array, and javase

Source: Internet
Author: User
Tags array definition

Chapter 1 of JAVASE, Chapter 3, program control and array, and javase
Purpose:
1. You can understand the three structures of the program.
2. You can understand one-dimensional arrays, two-dimensional arrays, multi-dimensional arrays, and irregular arrays.
3. You can understand Shift Operators and random numbers.


Three structures of 1 program
Code execution from top to bottom in a sequential structure
Select the statement to determine the execution sequence of the code based on the logic
Loop statements repeatedly execute a code block based on logic


2 SELECT statement
1) if statement
1/Direct output
2/if... else
3/if... else if...
4/if, for example, (a> B )? A/B: a * B


2) switch statement
Switch statement: Only byte, int, char, and short enumeration types are allowed.
Statement format: switch (variable)
{
Case variable 1: Statement Block 1; break;
Case variable 2: Statement Block 2; break;
Default: statement;
}


3. Loop statements
1) while statement
Statement features: Judge first and then execute a loop


2) do... while statement
Statement features: execute a loop at least once before determining


3) for Loop


4 keywords
Break: jump out of the loop body and close the loop directly.
Continue: abort this loop, directly next loop.


5 arrays
1) array declaration and initialization
A Dynamic Expression int [] array name = new int [number of arrays]
B static initialization int [] array name = new int [] {array value 1, array value 1 ...}
Int [] array name = {array value 1, array value 1 ...}
2) array subscript (also called array index)
The subscript of array a starts from 0.
B. Do not access non-existing array elements, which exceed the orientation of array elements.


3) Length of the array
A uses array name. length to get the array length.
B code, length Traversal


4) default and initialization of Arrays
A defines an array but does not assign values to the array. In this case, the system will initialize the array by default.
B Note: The default value of byte short int float dobuble is 0, and the default value of Boolean is flase.


5) Conversion
A converts a string to a character.
String str = "strValue ";
Char charVal = str. charAt (0 );


B. Convert the string to a struct array.
String str = "strValue ";
Char [] charArray = str. toCharArray ();


6) two-dimensional array
A display Initialization
1/int [] [] results = {1, 2, 3}, {1, 2, 3}; // {initial value of the first row}, {initial value of the second row }, {initial value of the third row }...}
2/int [] [] results = new int [] [] {1, 2, 3}, {1, 2, 3 }};


B Application
1/matrix conversion => convert rows and column elements of a two-dimensional array and store them in another two-dimensional array.
2/Yang Hui triangle => its oblique edge is composed of numbers 1, while the remaining number is the sum of the two numbers on his shoulder.


C understanding
1/array: Used as a linear graph in ry.
2/two-dimensional array: equivalent to a table


7) multi-dimensional array
The dimension of the array is the logarithm of brackets after the type.
Number of multi-dimensional array elements = the product of the size on each two-dimensional


8) Irregular array and rule Array
Rule array: linear distribution, with the same number of elements per line
Irregular array: the number of elements in each row is different.
A irregular array definition: Because multidimensional arrays are not linearly allocated in memory, they can exist in irregular forms.
B, such as 1/int [] [] results = {1, 2, 3}, {5, 7}, {0 }}
2/int [] [] x = new int [5] []
Row 0 has four elements => x [0] = new int [4] 1 has three elements => x [1] = new int [3]...


6 Shift Operators
> Shifts to the right once, which is equal to dividing by 2 (moving two places to the right and adding 1 to the right)
<Shifts to the left once, which is equivalent to multiplying 2 (shifts two places to the left, and adds 0 to the left)
>>> Shifts two places to the right, and adds 0 to the left (no <operator)


7 small applications
A generates a random number.
Int num = new java. util. Random (). nextInt (10); // Random Number 1-10


B input from the keyboard. An input box is displayed.
String obj_str = javax. swing. JOptionPane. showInputDialog ("input number :");
Int num_int = Integer. parseInt (obj_str );
Double num_dou = Double. parseDouble (obj_str );

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.