当前位置:网站首页>Day_ 04
Day_ 04
2022-06-25 16:25:00 【grp_ grp_ grp】
day_04 Array
1. Array
An array is a reference type , Used to store multiple data
1.1 data structure
Data structure is computer storage 、 How to organize data . Data structure refers to the collection of data elements that have one or more specific relationships with each other . 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
1.2 Array properties
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
1.3 Array declaration
Array declaration
1 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}
};
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 , For example, in the above program Will save 5 individual 0
Integers 0 , decimal 0.0 , Boolean false , character \u0000 , Reference type null

1.4 storage


1.5 Using an array
1.5.1 get data

1.5.2 Change data

1.5.3 Traverse

1.5.4 Common anomalies
The subscript crossing the line


Null pointer


1.5.5 Array passing

1.6 Value and address


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. 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);
边栏推荐
- f_read 函数[通俗易懂]
- Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution
- Create raspberry PI image file of raspberry pie
- 心樓:華為運動健康的七年築造之旅
- 什么是骨干网
- JS add custom attributes to elements
- Uncover gaussdb (for redis): comprehensive comparison of CODIS
- 报错:homebrew-core is a shallow clone
- The style of the mall can also change a lot. DIY can learn about it!
- Xinlou: un voyage de sept ans de Huawei Sports Health
猜你喜欢

Blue Bridge Cup - practice system login

Navicat Premium 15 for Mac(数据库开发工具)中文版

Android修行手册之Kotlin - 自定义View的几种写法

说下你对方法区演变过程和内部结构的理解

iVX低代码平台系列详解 -- 概述篇(一)

Go language - what is critical resource security?

Sleep formula: how to cure bad sleep?

What processes are needed to build a wechat applet from scratch?

Linux-MySQL数据库之高级SQL 语句一
Inter thread synchronization semaphore control
随机推荐
Time wheel and implementation analysis of time wheel in go zero
When inputting text in the shutter textfield, if the page is refreshed, the cursor position will change.
Converting cifar10 datasets
Educational administration system development (php+mysql)
Process control and method
Mt60b1g16hc-48b:a micron memory particles FBGA code d8bnk[easy to understand]
Swift responsive programming
mysql整体架构和语句的执行流程
Sleep formula: how to cure bad sleep?
Flutter textfield setting can input multiple lines
cmd。。。。。。
Mysql database multi table query
有哪些新手程序员不知道的小技巧?
What are the reasons why the game industry needs high defense servers?
Prototype mode
Catheon Gaming任命Activision Blizzard前亚太区负责人Mark Aubrey担任首席执行官
The textfield is encapsulated by the flutter itself, which causes the data display to be disordered when the data in the list is updated.
八种button的hover效果
Principle analysis of ThreadLocal source code
f_ Read function [easy to understand]
