当前位置:网站首页>Common knowledge of one-dimensional array and two-dimensional array

Common knowledge of one-dimensional array and two-dimensional array

2022-07-07 12:35:00 Xiaobai shelter

Just a quick tip : Enter a number from the console
import java.util.Scanner // Import jar package
Scanner scanner =new Scanner(System.in)
System.out.println(“ Please enter a number :”);
// Get the entered number
int input=scanner.nextlnt();
 Insert picture description here
One : One dimensional array
1. Definition : Arrays are reference data types , Used to store multiple data . Reference data types also include classes and interfaces
2. data structure : Data structure is computer storage 、 How to organize data . Data structure refers to the existence of one or more A collection of data elements with specific relationships . Usually , Well chosen data structure can bring higher operation or storage efficiency . Data structures are often related to efficient retrieval algorithms and indexing techniques

  Data manipulation  :  Additions and deletions 

3. Array properties
Continuous storage in memory , And subscript from 0 Start ( Memory address offset )

  •   Once the array length is determined   The length cannot be changed , This means that arrays cannot be added or deleted 
    
  •   Unless you create a new array , Copy the original data into the new array , In the process of copying, you can add and delete 
    
  •   therefore   Array query and change   Efficiency is very high , however   Adding and deleting are inefficient 
    
  •   Arrays have a built-in property  length   Saved the length of the array 
    
  •   stay java in   There is one java.util.Arrays  class   Provides some array operations 
    

4. Array declaration

  •  ①  Static declaration  :  When each element is known , Use static declarations 
    
  •   data type    Variable name  =  value ;
    
  •  		int i = 2;
    
  •  		 data type []   Variable name  = { value , value , value .....};   Shorthand way 
    
  •  		 data type []   Variable name  = new  data type []{ value , value , value ......};   For example, assign a value to an array twice , You need to write 
    
  •  		int[] arr = {1,2,3,6,1,2,3};
    
  •  		int[][] arr = {
    
  •  					{1,2,3},{2,1,3},{4,6}
    
  •  			};
    
  •  ②  Dynamic statement  :  When you don't know every element in the array , Use dynamic declarations 
    
  •  		 data type []  Variable name  = new  data type [ length ];
    
  •  		int[] arr = new int[5];
    
  •  		int[][] arr = new int[2][3];
    
  •  		 Dynamic statement , The default value of the corresponding type is saved , For example, in the above program   Will save 5 individual 0
    
  •  		 Integers   0  ,  decimal   0.0  ,  Boolean  false ,  character  \u0000 ,  Reference type  null
    
         int[] arr={1,2,3};  // Static declaration 
          If you assign a value to an array twice , It must be written like this , Don't write directly ------ arr=new int[] {2,4,3} 
         int [] arr2=new int[10]   Also can put the “[]” Write after the variable name    int arr2[] =new int[10]
    

5. get data
int[] arr ={10,11,12,13} // Statically initialize an array
System.out.println(arr[0]); // Note subscript from 0 Start Represents the first number
System.out.println(arr[1]);
System.out.println(arr[2]);
6. Change data
Array [ Little sign ]= value ;
arr[1]=1;
System.out.println(arr[1]);
7. Traverse
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}

// enhance for loop foreach
// Put every element in the array , All assigned to variables
//for( data type Variable name : Array ){}
for(int element:arr){
System.out.println(element);
}
8. Common abnormal
The subscript crossing the line
 Insert picture description here
Null pointer
 Insert picture description here
 Insert picture description here

 Insert picture description here
9. Array passing
example :m2(new int[]{1,2,3});
10.Main Method dissemination
 Insert picture description here
 Insert picture description here 10. Value transfer refers to the transfer of basic types of data After basic type data transmission , There is no influence on each other , Because it points to different spaces
Byref / The reference It refers to passing the value of the reference data type After the reference type is passed , Influence each other , Because it points to the same heap Save object space
11. Array copy
public static int[] copy(int[] src, int srcPos, int[] dest, int destPos,
int length) {
}
src: Source array srcPos: Source array start position dest: Target array destPos: Starting position of target array length: Insert number
notes : Because it's insert copy , So the length of the array will certainly be changed , Because the array length cannot be changed , So we can only create new arrays , And return the array through the return value

Two : Two dimensional array
1. Declaration method
 Insert picture description here
 Insert picture description here
2. storage

 Insert picture description here
 Insert picture description here
 Insert picture description here
3. get data
int[] arr0 = arr4[0]; // Put the array arr4 The first value in is given to arr0 This array
int arr00 = arr0[0]; // Put the array arr0 The first value of is given to arr00 This array
System.out.println(arr00);// Output arr00 This array
System.out.println(arr4[0][0]);
System.out.println(arr4[3][1]);
4. Change data
change Array [ Subscript ][ Subscript ]= value
arr4[3][1]=33;
5. Traverse
 Insert picture description here
6. Dynamic declaration zigzag

 Insert picture description here
7. Exchange the values of variables
int x = 10;
int y = 11;
// 1 Intermediate variable ( Development commonly used )
int temp = x;
x = y;
y = temp;
System.out.println(“x=” + x + “,y=” + y);

	// 2  Displacement calculation ( For interview )
	x = 2; // 0000 0010
	y = 3; // 0000 0011
	//  Convert to the corresponding binary , Every XOR , Take the same 0, Different take 1
	x = x ^ y; // 0000 0001
	y = x ^ y; // 0000 0010
	x = x ^ y; // 0000 0011
	System.out.println("x=" + x + ",y=" + y);

	// 3  Addition and subtraction 
	x = 10;
	y = 20;
	x = x + y;
	y = x - y;
	x = x - y;
	System.out.println("x=" + x + ",y=" + y);
原网站

版权声明
本文为[Xiaobai shelter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130617040903.html