Java Foundation Fourth day

Source: Internet
Author: User
Tags array length switch case

# # #01switch语句解构
* A:switch Statement Deconstruction
* A:switch can only be judged on the value of an expression, which determines which piece of code the program executes.

* B: The format is as follows:
Swtich (expression) {
CASE constant 1:
The statement to execute;
Break

Case constant 2:
The statement to execute;
Break

Case Constant 3:
The statement to execute;
Break

Default
The statement to execute;
Break
}
* C: Execution flow: the expression, compared to the constants following the case and the same as the one after the case, executes the program following the case, and when it encounters a break, it is all over.

* d: Keyword: switch case default break

* E: Example
If equals 1, the output is Monday
If equals 2, the output Tuesday
If equals 3, the output is Wednesday
If equals 4, the output Thursday
If equals 5, the output is Friday
If equals 6, the output is Saturday
If equals 7, the output is Sunday


# # # #02switch语句的星期判断
* A:switch Statement of the Week judgment
* A: Explicit requirements
Requirements: Initialize the INT type variable (1-7) represents the day of the week, using the switch statement to judge, and print out the corresponding week of the integer.

* B: Code implementation
public class SwitchDemo01 {
public static void Main (string[] args) {
int week = 5;
Switch (week) {
Case 1:
System.out.println ("Monday");
Break
Case 2:
System.out.println ("Tuesday");
Break
Case 3:
System.out.println ("Wednesday");
Break
Case 4:
System.out.println ("Thursday");
Break
Case 5:
System.out.println ("Friday");
Break
Case 6:
System.out.println ("Saturday");
Break
Case 7:
System.out.println ("Sunday");
Break
Default:
System.out.println ("The number entered is incorrect ...");
Break
}
}
}


# #03switch语句接受的数据类型
* A:switch statement accepted data type
* A: NOTE
The data type of the expression in the switch statement is required
jdk1.0-1.4 data type accepts a byte short int char
JDK1.5 data type accepts a byte short int char enum (enum)
JDK1.7 data type accepts a byte short int char enum (enum), String

# # #04case穿透
* A:case penetration
* A: In the process of using a switch statement, if the execution statement after multiple case conditions is the same, then the execution statement can only be written once, which is a shorthand way.
* B: For example, to determine whether a day of the week is a weekday, also use a digital 1~7 to indicate Monday to Sunday, when the number entered is 1, 2, 3, 4, 5 o'clock is considered a weekday, otherwise it is considered a day off.

# # #05数组的概述
* A: An overview of arrays
* A: Requirements for arrays
It is now necessary to count the wages of a company's employees, such as the average wage and the highest wage. Assuming that the company has 50 employees, using the knowledge learned above,
Then the program first needs to declare 50 variables to remember each employee's salary separately, this will be very troublesome.

* B: an overview of arrays
An array is a set of data that each of the data in an array is called an element. Any type of element can be stored in an array, but the type of elements stored in the same array must be identical.

# # #06数组的定义
* A: Definition of an array
* B: Format:
data type [] Array name = new data type [element number or array length];

* C: for example:
int[] x = new INT[100];
* C: Key notes
1) Data type: The data type of the element stored in the array
2) [] indicates the meaning of the array
3) variable name custom identifier
4) New Create container keyword
5) Data type: The data type of the element stored in the array
6) [] indicates the meaning of the array
7) The number of elements, that is, the array, how many data can be stored (constant, fixed length)

An array is a container: Each element stored in an array has its own AutoNumber
Auto number, minimum value is 0, maximum, length-1
Auto-numbered professional ranking, index, subscript, corner label
Accessing an array-stored element must depend on the index, the formula array name [index]

Java provides a property that operates on an indexed
An attribute of an array is the length of the array, the name of the property, length
Use attribute: array name. length data type int

The minimum index of an array is 0, the maximum index array. length-1

# # #07JVM内存划分
* A: Memory Partitioning
* The JVM divides its own memory into 5 regions
* A: Register: between memory and cup
* B: Local method Stack: The JVM invokes the functionality in the system
* C: Method and data sharing: where the runtime class file enters
* D: Method Stack: All methods run when entering memory
* E: Heap: Storage of containers and objects


# # #08数组的内存
* A: Array of memory
* int[] x; Declaring a variable of type int[]
*x = new int[100];//creates an array of length 100
* Next, use two memory diagrams to detail the allocation of memory during the creation of the array.
* First line of code int[] x; Declares a variable x, which is of type int[], an array of type int. The variable x occupies a block of memory that is not assigned the initial value
* The second line of code x = new INT[100]; An array is created that assigns the address of the array to the variable x. You can use variable x to reference an array while the program is running, and the state in memory changes



# # #09使用索引访问数组的元素
* A: Accessing elements of an array using an index
* There are 100 elements in the group with an initial value of 0. Each element in the array has an index (also called a corner label), and the elements in the array can be accessed in the form of "x[0", X[1] 、......、 x[98], x[99] ".
* Note that the smallest index in the array is 0, and the largest index is "array length-1"

# # #10数组的length属性
* A:lenth Property
* A in Java, in order to facilitate us to obtain the length of the array, provides a length property, in the program can be "array name. Length" Way to get the length of the array, That is, the number of elements.
* b to find the length of the array
public class ArrayDemo01 {
public static void Main (string[] args) {
int[] arr;//declare variable
arr = New Int[3]; Create an Array object
System.out.println ("arr[0]=" + arr[0]);//access the first element in the array
System.out.println ("arr[1]=" + arr[1]);//access the array The second element of
System.out.println ("arr[2]=" + arr[2]);//access to the third element in the array
System.out.println ("The length of the array is:" + arr.length); Print array length
}
}

# # #11为数组的元素赋值
* A: Assigning elements to an array
* A: If you do not want to use these default initial values when using arrays, you can also explicitly assign values to these elements.
* The value of the assigned element has been changed to a new value, and the value of the element with no assignment is initialized by default
* B: Case
public class ArrayDemo02 {
public static void Main (string[] args) {
int[] arr = new INT[4]; Defines an array that can store 4 integers
Arr[0] = 1; Assigns a value of 1 to the 1th element
ARR[1] = 2; Assigns a value of 2 to the 2nd element
The following code is the value of each element in the printed array
System.out.println ("arr[0]=" + arr[0]);
System.out.println ("arr[1]=" + arr[1]);
System.out.println ("arr[2]=" + arr[2]);
System.out.println ("arr[3]=" + arr[3]);
}
}


# # #12数组的定义_2
* A: Define array Format 2
* A: Array initialization
Dynamic initialization: Specifies only the length of the array when the array is defined, and the way the system automatically assigns an initial value to an element is called dynamic initialization.
1. Type [] Array name = new type [length];
int[] arr = new INT[4];
Static initialization: There is also a way to initialize an array, called static initialization, to assign values to each element of an array while it is being defined.
2. Type [] Array name = new Type []{element, element, ...};
int[] arr = new int[]{1,2,3,4};
3. Type [] Array name = {element, element, element, ...};
Int[] arr = {1, 2, 3, 4};


# # #13遍历数组
* A: Iterating through an array
* When manipulating arrays, it is often necessary to sequentially access each element in the array, which is called an array traversal
* B: Practice
public class ArrayDemo04 {
public static void Main (string[] args) {
Int[] arr = {1, 2, 3, 4, 5}; Defining arrays
Iterating through the elements of an array using a For loop
for (int i = 0; i < arr.length; i++) {
System.out.println (Arr[i]); Accessing elements by index
}
}
}
In the code above, define an array of length 5 arr, with the array's corner marked as 0~4. Because the value of the variable I defined in the For loop is 0~4 during the loop, it can be indexed, go to the elements in the array, and print the value of the element


# # #14数组中常见的异常
* A: Two common exceptions in array operations
Array index out of bounds exception
Null pointer exception

* B: Practice
public class arraydemo_4{
public static void Main (string[] args) {
Array index out of bounds exception
Int[] arr = {5,2,1};
Array of 3 elements, index 0,1,2
System.out.println (Arr[3]);//java.lang.arrayindexoutofboundsexception:3

Null pointer exception
Int[] arr2 = {1,5,8};
System.out.println (arr2[2]);
ARR2 = null; ARR2 not save the address of the array
System.out.println (arr2[2]);//java.lang.nullpointerexception
}
}

# # #15数组最值
* A: The idea of how to get the maximum value of an array
* Define the first element of the array arr[0] is the maximum value; loop arr Array, judging if there is a larger than arr[0] swap, until the ARR array is traversed, then the largest element is saved in arr[0]


# # #16数组获取最值代码实现
* A: Code implementation
public class ArrayDemo05 {
public static void Main (string[] args) {
Int[] arr = {4, 1, 6, 3, 9, 8}; Define an array
int max = arr[0]; Define variables Max is used to remember the maximum number, first of all assuming the first element is the maximum value
The following iterates through the elements in the array through a for loop
for (int x = 1; x < arr.length; × x + +) {
if (Arr[x] > Max) {//compare Arr[x] The value is greater than max
max = arr[x]; Conditional, assign the value of Arr[x] to Max
}
}
System.out.println ("max=" + max); Print Maximum Value
}
}


# # #17二维数组的定义
* Function of a two-dimensional array
* To count the test scores of each class in a school, how can we achieve them?
* A multidimensional array is required, and a multidimensional array can be simply interpreted as nesting arrays in an array.
* B Definition Format
* a the first definition of the format:
* int[][] arr = new INT[3][4];
* The above code is equivalent to defining a two-dimensional array of 3*4, that is, the length of the two-dimensional array is 3, and each element in a two-dimensional array is an array of length 4
* b The second definition of the format
* int[][] arr = new int[3][];
* The second type is similar to the first, except that the length of each element in the array is indeterminate
* Third definition format of C
* int[][] arr = {{1,2},{3,4,5,6},{7,8,9}};
* Three elements are defined in a two-dimensional array, all three elements are arrays, {}, {3,4,5,6}, {7,8,9}

# # #18二维数组元素的访问
* A: Access to two-dimensional arrays
* Case:
class ArrayDemo08 {
public static void Main (string[] args) {

//fixed The way of a two-dimensional array
int[][] arr = new Int[3][4];
System.out.println (arr);
System.out.println ("Length of the two-dimensional array:" + arr.length);
//Get 3 elements of a two-dimensional array
System.out.println (arr[0]);
System.out.println (arr[1]);
System.out.println (arr[2]);

System.out.println ("prints element values of the first one-dimensional array");
System.out.println (Arr[0][0]);
System.out.println (arr[0][1]);//access to the 2nd element of the 1th one-dimensional array in a two-dimensional array
System.out.println (arr[0][2]);
System.out.println (Arr[0][3]);

System.out.println ("prints element values of the second one-dimensional array");
System.out.println (Arr[1][0]);
System.out.println (arr[1][1]);
System.out.println (arr[1][2]);
System.out.println (arr[1][3]);

System.out.println ("prints element values of the third one-dimensional array");
System.out.println (Arr[2][0]);
System.out.println (arr[2][1]);
System.out.println (arr[2][2]);
System.out.println (arr[2][3]);
}
}


# # #19二维数组内存图
* A: Two-dimensional array memory graph
* Example: int[][] arr = new int[3][2];
* The outer number of the leader in memory open up a continuous 3 large memory space, each memory space corresponding to the address value
* Each large memory space opens up two contiguous small memory spaces.

# # #20二维数组的定义和访问
* A: Definition and access of two-dimensional arrays
* Format 1:
* int[][] arr = new int[3][]; Not recommended
* Format 2
* int[][] arr = {{1,2,4},{4,7},{0,9,3}};
*
* B: Access to two-dimensional arrays
Example: int[][] arr = {{1,2,4},{5,8,7},{0,9,3}};
To print an array of 7 this element needs to first find the large element index {5,7} index to 2, and 7 in the {5,7} index 2
The result is arr[2][2] the first [2] represents the index of the element {5,8,7} in a large array
The second [2] represents the index of the 7 element in {5,8,7}

# # #22二维数组的遍历
* A: Two-dimensional array traversal
Int[][] arr = {{1,2,4},{4,7},{0,9,3}};
Use the For loop to iterate through the two-dimensional array of arr and get each element arr[i] as a one-dimensional array
The outer for loop is nested within a for loop that iterates through each of the one-dimensional arrays arr[i], getting each element

*b: Example: Traversing a two-dimensional array
public class arrayarraydemo_2{
public static void Main (string[] args) {
Int[][] arr = {{1,2,3},{4,5},{6,7,8,9},{0}};

Outer Loop, traversing a two-dimensional array
for (int i = 0; i < arr.length; i++) {
Inner loop, traversing each one-dimensional array arr[0] arr[1] arr[i]
for (int j = 0; J < Arr[i].length; J + +) {
System.out.print (Arr[i][j]);
}
System.out.println ();
}
}

* C: Two-dimensional array summation sum
Class ArrayDemo09 {
public static void Main (string[] args) {
Int[][] arr2 = {{1,2},{3,4,5},{6,7,8,9,10}};
int sum2 = 0;
for (int i=0; i<arr2.length; i++) {
for (int j=0; j<arr2[i].length; J + +) {
System.out.println (Arr2[i][j])
Sum2 + = Arr2[i][j];
}
}
System.out.println ("sum2=" + sum2);
}
}

# # #23二维数组的求和练习
* A for example, to count the total sales of each group in a company's three sales groups and the sales of the company as a whole. As shown below
* First group sales are {11, 12} million
* Sales for the second group are {21, 22, 23} million
* The third group sales are {31, 32, 33, 34} million.

* B Code implementation
public class ArrayDemo10 {
public static void Main (string[] args) {
int[][] arr = new int[3][]; Define a two-dimensional array of length 3
Arr[0] = new int[] {11, 12}; Assigning a value to an element of an array
ARR[1] = new int[] {21, 22, 23};
ARR[2] = new int[] {31, 32, 33, 34};
int sum = 0; Define variables record Total sales
for (int i = 0; i < arr.length; i++) {//iterate over array elements
int groupsum = 0; Define variables record group sales total
for (int j = 0; J < Arr[i].length; J + +) {//traverse each person's sales in the group
Groupsum = Groupsum + arr[i][j];
}
sum = sum + groupsum; Cumulative Group Sales
System.out.println ("No." + (i + 1) + "group sales:" + groupsum + "million");
}
SYSTEM.OUT.PRINTLN ("Total sales:" + sum + "million");
}
}

# # #24随机点名器案例分析
* A Random Register case study

* B: Demand
* Random register, that is, in the class randomly print out a student's name.

* C: Analysis:
* 1) Definition of array storage the whole class.
* 2) Generate random number range 0 to array length-1
* 3) Find the names of the students in the array according to this index

# # #25随机点名器代码实现
* A: Analysis
Random Name Register:
1 Store Name
2. Preview everyone's name
3. Random name of a person
* B Code implementation
Import Java.util.Random;
public class callname{
public static void Main (string[] args) {
Store name, name stored in array
Array store name, name data type, String
String[] names = {"Zhang San", "John Doe", "Harry", "Li Lei", "Han Meimei", "nickname", "Lao Wang", "Xiao Hua", "Johnson", "Alice"};

Preview: Iterate through the array and print all the names
for (int i = 0; i < names.length; i++) {
System.out.println (Names[i]);
}
System.out.println ("=============");

Randomly out of a person's name
Use random numbers to generate an integer, as an index, to find the corresponding element in the array
Random ran = new random ();
Random number, the range must be 0-the maximum index of the array
int index = Ran.nextint (names.length);//index is a random number, as an index
System.out.println (Names[index]);
}
}

# # #25随机点名器代码实现_2
* A code optimization:
Import Java.util.Random;
public class callname{
public static void Main (string[] args) {
String[] names = {"Zhang San", "John Doe", "Harry", "Li Lei", "Han Meimei", "nickname", "Lao Wang", "Xiao Hua", "Johnson", "Alice"};
System.out.println (Names[new Random (). Nextint (Names.length)]);
}
}

Java Foundation Fourth day

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.