当前位置:网站首页>Discussion on some problems of array
Discussion on some problems of array
2022-07-03 07:06:00 【L gold p】
1、 Array
Reference data type
Definition : Used to store multiple data
1.1 data structure
Definition : data structure It's computer storage 、 How to organize data . data structure A collection of data elements that have one or more specific relationships with each other . Usually , Carefully chosen data structure It can bring higher operation or storage efficiency . data structure It is often related to efficient retrieval algorithms and indexing techniques .
operation : Additions and deletions
1.2 Array properties
Continuous storage in memory , And subscript from 0 Start
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
So the query and change of array It's very efficient But the efficiency of adding and deleting is very low
Array has a built-in property length Saved the length of the array
stay Java There is one of them. java.util.arrays class Provides some data operations
1.3 Array declaration
1 Static declaration : When you know what each element is , 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,1,1,122,0};
——int [][] arr={
{1,1,2} {2,5,6} };
2 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, such as in the following program Five will be saved 0
Integers 0 decimal 0.0, Boolean false, character \u0000, Reference type null
for example 
1.4 storage


1.5 Using an array
1.4.1 get data

1.5.2 Change data

1.5.3 Traverse

1.5.4 Common abnormal
1、 Null pointer


2、 The subscript crossing the line


1.5.5 Array passing

1.5.6Main Method dissemination


1.6 Value and address
1、 Pass value It refers to passing values of basic types , Mutual indifference , Because it points to different spaces
2、 Byref / The reference It refers to passing the value of reference type , Influence each other , It refers to the same heap of memory object space

1.7 Array copy

public static void main(String[] args) {
int[] src = { 2, 3, 4, 5, 6, 7, 8 };
int[] dest = { 11, 12, 13, 14, 15, 16, 17, 18 };
// int[] dest = { 11, 12, 13, 14,4, 5, 6, 15, 16, 17, 18 };
int[] result = copy(src, 2, dest, 3, 3);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
/**
* Insert replication
*
* @param src
* Source array
* @param srcPos
* Source array start position
* @param dest
* Target array
* @param destPos
* Starting position of target array
* @param length
* Insert number
* @return
*/
public static int[] copy(int[] src, int srcPos, int[] dest, int destPos,
int length) {
// 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
/**
* 0 Create a new array , The length is dest.length + length
*
* 1 In the target array from 0 Start To Start position end ( contain ), Copy to a new array
*
* 2 In source array Starting from the starting position ( contain ) Copy length individual To In the new array
*
* 3 In the target array From the starting position +1 Start ( contain ) In the end All data Copy to a new array
*/
// 0 Create a new array , The length is dest.length + length
int[] newDest = new int[dest.length + length];
// 1 In the target array from 0 Start To Start position end ( contain ), Copy to a new array
for (int i = 0; i <= destPos; i++) {
newDest[i] = dest[i];
}
// 2 In source array Starting from the starting position ( contain ) Copy length individual To In the new array
// Now you need to use the starting position of the target array and change , however The starting position of the target array is also used later , So we can't change
// therefore Here, the starting position of the target array Assign to a new variable index, You can change index
int index = destPos;
for (int i = srcPos; i < srcPos + length; i++) {
index++;
newDest[index] = src[i];
}
// 3 In the target array From the starting position +1 Start ( contain ) In the end All data Copy to a new array
for (int i = destPos + 1; i < dest.length; i++) {
index++;
newDest[index] = dest[i];
}
return newDest;
}
2. Two dimensional array
2.1 Declaration method

2.2 storage


2.3 Usage mode
2.3.1 get data

2.3.2 Change data
2.3.3 Traverse

2.3.4 Dynamic declaration zigzag

3. The value of the variable
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);
边栏推荐
- Golang operation redis: write and read kV data
- Golang operation redis: write and read hash type data
- Software testing assignment - day 3
- HMS core helps baby bus show high-quality children's digital content to global developers
- php artisan
- Use the jvisualvm tool ----- tocmat to start JMX monitoring
- UTC time, GMT time, CST time
- Asynchronous programming: async/await in asp Net
- How does the insurance company check hypertension?
- Search engine Bing Bing advanced search skills
猜你喜欢

La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ
![[classes and objects] explain classes and objects in simple terms](/img/41/250457530880dfe3728432c2ccd50b.png)
[classes and objects] explain classes and objects in simple terms

Pytest -- write and manage test cases

In depth analysis of reentrantlock fair lock and unfair lock source code implementation

Software testing assignment - day 1

2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution

Mise en place d'un environnement de développement de fonctions personnalisées

How to migrate or replicate VMware virtual machine systems

Journal quotidien des questions (11)

Inno setup production and installation package
随机推荐
Inno setup production and installation package
691. Cube IV
Advanced API (multithreading)
crontab定时任务
[classes and objects] explain classes and objects in simple terms
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
深度学习参数初始化(一)Xavier初始化 含代码
UTC时间、GMT时间、CST时间
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
UTC time, GMT time, CST time
Error c2017: illegal escape sequence
Winter vacation work of software engineering practice
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Laravel框架 踩坑(一)
EasyExcel
Inno Setup 制作安装包
Book recommendation~
Laravel Web框架
2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
LeetCode