1. Array overview, definition Format
* A: Array concept 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. * B: Array definition Format format 1: Data type [] array name; ----int[] arr; Format 2: Data type array name []; ----int arr[]; * C: An array is something (a container) that stores multiple variables (elements), and the data types of the multiple variables are consistent,
2. Array initialization, dynamic initialization
*A: What is the initialization of an array in Java the array must first be initialized before it can be used. Initialize: Allocates the memory space for array elements in arrays and assigns values to each array element. *B: How to initialize an array* A: Dynamic initialization--Specifies the length only, the system gives the initialization value*format: Data type [] array name=NewData type [5]; *int[] arr =New int[3]; Explanation: An array of type int is defined, which can hold a value of 3 int type. * B: Static initialization--Given the initialization value, the system determines the length of the*format: Data type [] array name=Newdata type []{element 1, element 2,...}; *The initial value of each array element is specified at initialization, and the array length is determined by the system. *int[] arr = {1,2,3,4,5,6,7,8,9,0}; *C: Dynamic initialization format:* data type [] Array name =Newdata type [array length]; *the length of the array is actually the number of elements in the array. *D: Case Demo*explanation of the logarithm group*output array names and arrays elements*E: Format of the length of the array* Array name. length
3.java memory allocation, stack, heap differences
* A: Stack---store local variables,* B: Heap---stores new things, and new objects within the heap have initialization values. *C: Method Area*D: Local method Area*E: Register1: All variables B of the local variable A: In the method definition or on the method declaration (for example: parameters): Finished, immediately disappear2:new out of something---------entity, object. New int[3]; A: Each entity has an initial address value B: Data in each entity has a default valuebyte, Short,int,Long--------0float,Double-----------------0.0Char' \u0000 '----------------Unicode encoding, 0 is the number of 16 binaryBoolean falseReference type:NULLC: After the use is complete, it will be reclaimed by the garbage collector when it is idle.
4. Array static initialization
* A: Statically initialized format: new data type []{element 1, element 2,...}; * simplified format: * data type [] Array name = {element 1, element 2,...};
5. Two common minor problems with array operations cross-border and null pointers
* 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.
6. Array Demo operation
Array Traversal: is the output of each element in the array sequentially. int [] arr={1,2,3,4,5,6,7,8}; for (int x=0;x<arr.length;x++) { System.out.println (arr[x]); }
array gets the maximum value (gets the smallest value in the array) public static int Getmax (int [] arr) { int [] arr={1,2,3,4,5,6,7,8} ; int max=arr[0]; for (int x=1;x<arr.length;x+ + if (Max<arr[x] {max =arr[x]; }} System.out.println (max); }
array element Inversion (two methods in reverse) (to swap elements)1. Public Static voidNixu () {int[] arr={1,2,3,4,5,6,7,8}; for(intstart=0,end=arr.length-1;start<end;start++,end--){ inttemp=Arr[start]; Arr[start]=Arr[end]; Arr[end]=temp; } } 2. Public Static voidniXu2 () {int[] arr={1,2,3,4,5,6,7,8}; 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)ImportJava.util.Scanner; classDemochabiao { Public Static voidMain (string[] args) {/*Requirements: Array lookup table Method (according to the keyboard input index, find the corresponding week) analysis: Enter a number from the keyboard,*/ //Creating ObjectsScanner sc =NewScanner (system.in); //keyboard input a number intx=Sc.nextint (); //defines an array of type char Char[] arr={', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' Day '}; CharC=Getweek (X,arr); System.out.println (c); System.out.println ("Hello world!"); } //definition Returns the day of the week character method, return value type char, parameter list int x,char[] Arr Public Static CharGetweek (intXChar[] arr) { Charc =Arr[x]; returnC; } }
array element Lookup (finds the index of the first occurrence of the specified element in the array)ImportJava.util.Scanner; classDemoisequals { Public Static voidMain (string[] args) {/*requirements: Array element Lookup (finds the index of the first occurrence of the specified element in the array) analysis: 1. Element traversal, 2. Compares the array element with the given value 3. Output */ int[] arr={1,2,3,4,5,6}; Scanner SC=NewScanner (system.in); intx =Sc.nextint (); intb=isequals (X,arr); System.out.println (b); System.out.println ("Hello world!"); } //define method, return value type Boolean, parameter list int x, Int[]arr Public Static intIsequals (intXint[] arr) { //The for loop is used to traverse and compare to see if the value of the array element is equal to the input data, equal to return the index, and not equal to return-1 for(inti= 0; i<arr.length; i++ ) { if(x==Arr[i]) { returni; } } return-1; } }
7. Overview of two-d arrays and their formats
*A: A Two-dimensional array overview*B: Two-dimensional array format 1* data type [] Array name =Newdata type [m][n]; *int[] arr=New int[m][n];*C: Interpretation of the two-dimensional array format 1*This is a two-dimensional array, which has m one-dimensional arrays, each one-dimensional array has n elements,*D: precautions*A: The following format can also represent a two-dimensional array* 1: Data type array name [] =Newdata type [m][n]; * 2: Data type [] array name [] =Newdata type [m][n]; *B: Note the differences defined below intx; inty; intx, y; int[] x; int[] y[]; int[] x,y[];
* A: Two-dimensional array format 2new data type [m][] ; int New int [3] [] ; New int [2]; New int [3] New int [1];
* A: Two-dimensional array format 3* new data type [][]{{element ...},{element ...},{element ...}}; * Simplified version format: * data type [] Variable name = {{element ...},{element ...},{element ...}};
8. Two-dimensional array demo
requirements: Two-dimensional array traversal int [] arr={{1,2,3,4},{2,3,4,5}}; for (int x=0;x<arr.length;x++) { for (int y=0;y<arr.length;y++ ) { System.out.println (arr[x][y]); } }
demand: Company Annual sales sum the data of a company by quarter and month are as follows: unit (million yuan) first quarter:22,66,44second quarter:77,33,88third quarter:25,45,65fourth quarter:11,66,99classDemo2array2 { Public Static voidMain (string[] args) {int[] arr={{22,66,44},{77,33,88},{25,45,65},{11,66,99}}; intSum=0; for(intx=0;x<arr.length; x + + ) { for(intY=0;y<arr[x].length; y++) {sum+=Arr[x][y]; }} System.out.println (sum); System.out.println ("Hello world!"); }
read the program to write the results, and draw the memory diagram to explain Public Static voidMain (string[] args) {intA = 10; intb = 20; System.out.println ("A:" +a+ ", B:" +b); Change (A, b); System.out.println ("A:" +a+ ", B:" +b); int[] arr = {1,2,3,4,5}; Change (arr); System.out.println (arr[1]); } Public Static voidChangeintAintb) {System.out.println ("A:" +a+ ", B:" +b); A=b; b= A +b; System.out.println ("A:" +a+ ", B:" +b); } Public Static voidChangeint[] arr) { for(intx=0; x<arr.length; X + +) { if(arr[x]%2==0) {Arr[x]*=2; }}} Summary: Whether or not Java is a value or a pass-through? 1, in Java, the value is also transmitted, the basic data type is passed the value, the reference data type is addressed (address)2,java is a value, not a pass-through, because the address value is also a value (High Commander support)
arrays, stacks, heaps (Java Basics v)