当前位置:网站首页>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);
边栏推荐
- CentOS switches and installs mysql5.7 and mysql8.0
- Advanced API (serialization & deserialization)
- Redis command
- Practical plug-ins in idea
- Advanced API (character stream & net for beginners)
- Application scenarios of Catalan number
- Setting up the development environment of dataworks custom function
- Thoughts on project development
- 多个全局异常处理类,怎么规定执行顺序
- 机械观和系统观的科学思维方式各有什么特点和作用
猜你喜欢
Integration test practice (1) theoretical basis
多个全局异常处理类,怎么规定执行顺序
Summary of remote connection of MySQL
Reading notes of "learn to ask questions"
Practice of enterprise ab/testing platform
4279. Cartesian tree
Win 10 find the port and close the port
Basic components and intermediate components
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
Liang Ning: 30 lectures on brain map notes for growth thinking
随机推荐
Use the jvisualvm tool ----- tocmat to start JMX monitoring
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
这两种驱蚊成份对宝宝有害,有宝宝的家庭,选购驱蚊产品要注意
DBNet:具有可微分二值化的实时场景文本检测
MySQL mistakenly deleted the root account and failed to log in
Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding
Troubleshooting of high CPU load but low CPU usage
PAT甲级真题1166
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
Summary of remote connection of MySQL
How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
Shim and Polyfill in [concept collection]
Jenkins
mysql误删root账户导致无法登录
Practical plug-ins in idea
Resthighlevelclient gets the mapping of an index
691. 立方体IV
Winter vacation work of software engineering practice
My 2020 summary "don't love the past, indulge in moving forward"
Interface learning