| Document Version |
Development Tools |
Test Platform |
Project Name |
Date |
author |
Notes |
| V1.0 |
|
|
|
2016.02.24 |
Lutianfei |
None |
Chapter III Java BASIC SyntaxMethod
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2…) { 函数体; return 返回值; }
- Detailed Explanation:
- Modifier, now with public static, after detailed.
- Return value type: is the data type of the functional result.
- Method Name: According to the naming rules, it is convenient for us to call.
- Parameters:
- Actual parameters: is actually involved in the operation.
- Formal parameters: Is the method definition, used to receive the actual parameters.
- Parameter type: is the data type of the parameter.
- Parameter name: Name of variable
- Method Body Statement: Is the code that completes the function
- Return: the End method.
- Return value: Is the result of the function, which is brought to the caller by return.
Method Overloading
Class funciontest{ Public Static void Main(string[] args) {//test1 ShortB1 =3; ShortB2 =4; System.out.println ("Short:"+compare (B1,B2)); }//*******************************************************////Function:byte type Public Static Boolean Compare(byteAbyteb) {System.out.println ("byte type");returnA==b; }//-------------------------------------------------------////*******************************************************////Function:short type Public Static Boolean Compare( ShortA Shortb) {System.out.println ("Short Type");returnA==b; }//-------------------------------------------------------////*******************************************************////Function:int type Public Static Boolean Compare(intAintb) {System.out.println ("int type");returnA==b; }//-------------------------------------------------------////*******************************************************////Function:long type Public Static Boolean Compare(LongALongb) {System.out.println ("Long Type");returnA==b; }//-------------------------------------------------------//
Array
- Array Concepts
- 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.
-
The definition format of an array
- Format 1: Data type [] array name;
- Format 2: Data type array name [];
- Note: These two definitions are done, and there are no element values in the array. You also need to initialize the elements of the array.
-
eg
- A:int[] A; Defines an array a variable of type int
- B:int a[]; Defines an array of a variables of type int
-
Note:
- 1: It is recommended to use the first format for the definition of two formats for arrays. Because the first kind of readability is stronger. The second kind of early time is really a lot of people use it. But fewer people are using it now.
- 2: As a fan of Java C # (the copycat of Java) no longer supports the second syntax format. More and more languages may abandon the second format.
-
Overview of array initialization:
- Arrays in Java must be initialized before they can be used .
- Initialize: allocates the memory space for array elements in arrays and assigns values to each array element.
-
How arrays are initialized
动态初始化: only the array length is specified when initializing, and the initial value is assigned by the system array.
静态初始化: Specifies the length of the 初始值 Array (more) for each array element when it is initialized.
-
Dynamic initialization: Only the array length is specified when initializing, and the initial value is assigned by the system array.
- Format:
数据类型[] 数组名 = new 数据类型[数组长度] ;
数组长度is actually the number of elements in the array.
- Example:
- int[] arr = new INT[3];
- Explanation: An array of type int is defined, which can hold a value of 3 int type.
newallocates memory space for an array.
-
Static initialization: Specifies the initial value of each array element when initialized, and the system determines the length of the array.
- Format:
- data type [] Array name = new data type []{element 1, element 2,...};
- Example:
- int[] arr = new int[]{1,2,3};
- Explanation: An array of type int is defined, which can hold 3 values of type int, and the values are three-to-one.
- A simplified notation
- Note: Dynamic initialization and static initialization cannot be performed at the same time. It is not possible to give an element at the same time as the given array length.
memory Allocations in Java
- Plot array Memory three arrays
/* Requirements: 1. Define the first array and assign values to the array elements when the definition is complete. After the assignment is complete, the output array name and element. 2, defines the second array, after the definition is complete, assigns the value to the array element. After the assignment is complete, the output array name and element. 3. Define the third array and give it the address value of the first array. (Note that the type is consistent), and the element is re-assigned by the name of the third array. 4. Output the first array name and element again. */Class arraydemo{ Public Static void Main(string[] args) {int[] arr =New int[3]; arr[0] = the; arr[1] = -; arr[2] = the; System.out.println (arr); System.out.println (arr[0]); System.out.println (arr[1]); System.out.println (arr[2]);int[] arr2 =New int[3]; arr2[0] = A; arr2[1] = -; arr2[2] = -; System.out.println (ARR2); System.out.println (arr2[0]); System.out.println (arr2[1]); System.out.println (arr2[2]);//A third array int[] Arr3 = arr; arr3[0] = -; arr3[1] = $; System.out.println (arr); System.out.println (arr[0]); System.out.println (arr[1]); System.out.println (arr[2]); }}
Two common small problems with array manipulation
- Array index out of bounds exception
- ArrayIndexOutOfBoundsException reason: Occurs when an index that does not exist in the array is accessed.
-
Null pointer exception
- NullPointerException reason: An array reference does not point to an entity, but when an element in an entity is manipulated (the array is already out of stack memory).
-
Eg:
int[] arr = {1,2,3};System.out.println(arr[3//报错:ArrayIndexOutOfBoundsExceptionnull;System.out.println(arr[0//报错:NullPointerException
Array Exercises
- Array traversal (sequentially outputting each element in the array)
- Array properties
length , you can get the array length.
int[] arr2 = {1,4,52,4,5,6,3,2,8,5,3,1,6,7,8,3,2};//length 属性for (int x=0;x<arr2.length;x++){ System.out.println(arr2[x]);}
- Array gets the maximum value (gets the minimum value in the array)
/ * Analysis: A: From the array of people looking for a reference (generally the first), by default he is the largest. B: Iterate through the other elements, one at a time and a reference to my comparison. */Class arraydemo{ Public Static void Main(string[] args) {int[] arr2 = {1,4, the,4,5,6, -,2, the,323,3, A,6, at,8,654,2};intmax = arr2[0];intMin = arr2[0]; for(intx=1; x<arr2.length;x++) {max = (max > arr2[x])? Max:arr2[x]; Min = (min < arr2[x])? MIN:ARR2[X];//System.out.println (arr2[x]);} System.out.println ("Max is:"+max); System.out.println ("Min is:"+min); }}
- Reverse the array elements (that is, swap the elements)
/ * Analysis: A: Will arr[0] and Arr[length-1] Exchange B: Will arr[1] and arr[length-2] exchange ... C: Just Do arr.length/2 * * *Class arraydemo{ Public Static void Main(string[] args) {int[] arr = {1,4, the,4,5,6, -,2, the,323,3, A,6, at,8,654,2}; for(intI=0; i<arr.length;i++) {System.out.print (arr[i]+" "); } System.out.println (); Arrreverse (arr); for(intI=0; i<arr.length;i++) {System.out.print (arr[i]+" "); } } Public Static void Arrreverse(int[] arr) { for(intx=0; x<arr.length/2; x + +) {inttemp = arr[x]; ARR[X] = arr[arr.length-1-X]; arr[arr.length-1-X] = temp; } }}
- Array Lookup Table method (according to the keyboard input index, find the corresponding week)
import java.util.Scanner;class ArrayDemo{ publicstaticvoidmain(String[] args){ String[] strArr = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}; new Scanner(System.in); System.out.println("Please input a number(0~6):"); int index = sc.nextInt(); System.out.println(strArr[index]); }}
Java Basic Learning Day fifth-methods and arrays