当前位置:网站首页>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);
边栏推荐
- New knowledge! The virtual machine network card causes your DNS resolution to slow down
- Software testing learning - the next day
- JUC forkjoinpool branch merge framework - work theft
- CentOS switches and installs mysql5.7 and mysql8.0
- How does the insurance company check hypertension?
- Advanced API (multithreading)
- EasyExcel
- 2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
- SecureCRT取消Session记录的密码
- Advanced API (use of file class)
猜你喜欢
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
卡特兰数(Catalan)的应用场景
Inno Setup 制作安装包
Daily question brushing record (11)
Reading notes of "learn to ask questions"
JMeter JSON extractor extracts two parameters at the same time
Jenkins
2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
Sorting out the core ideas of the pyramid principle
Software testing learning - day 3
随机推荐
dataworks自定义函数开发环境搭建
Unit test notes
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
Error c2017: illegal escape sequence
Strategy mode
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
Advanced API (serialization & deserialization)
4279. 笛卡尔树
Selenium key knowledge explanation
Pytest -- write and manage test cases
SecureCRT取消Session记录的密码
PHP install composer
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
691. 立方体IV
2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
Sorting out the core ideas of the pyramid principle
JMeter JSON extractor extracts two parameters at the same time
DBNet:具有可微分二值化的实时场景文本检测
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*