当前位置:网站首页>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();
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 deletedUnless you create a new array , Copy the original data into the new array , In the process of copying, you can add and deletetherefore Array query and change Efficiency is very high , however Adding and deleting are inefficientArrays have a built-in property length Saved the length of the arraystay 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 declarationsdata type Variable name = value ;int i = 2;data type [] Variable name = { value , value , value .....}; Shorthand waydata type [] Variable name = new data type []{ value , value , value ......}; For example, assign a value to an array twice , You need to writeint[] 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 declarationsdata 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 0Integers 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 
Null pointer 


9. Array passing
example :m2(new int[]{1,2,3});
10.Main Method dissemination 
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 

2. storage



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 
6. Dynamic declaration zigzag

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);
边栏推荐
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
- 金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
- Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
- SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
- 2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
- idea 2021中文乱码
- 即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
- SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
猜你喜欢
![[pytorch practice] image description -- let neural network read pictures and tell stories](/img/39/b2c61ae0668507f50426b01f2deee4.png)
[pytorch practice] image description -- let neural network read pictures and tell stories

Static vxlan configuration

wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6

Attack and defense world - PWN learning notes

(to be deleted later) yyds, paid academic resources, please keep a low profile!

Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool

【统计学习方法】学习笔记——支持向量机(下)

SQL Lab (41~45) (continuous update later)

Hi3516 full system type burning tutorial

消息队列消息丢失和消息重复发送的处理策略
随机推荐
(待会删)yyds,付费搞来的学术资源,请低调使用!
gcc 编译报错
解密GD32 MCU产品家族,开发板该怎么选?
Inverted index of ES underlying principle
30. Feed shot named entity recognition with self describing networks reading notes
Hi3516 full system type burning tutorial
牛客网刷题网址
About sqli lab less-15 using or instead of and parsing
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
[pytorch practice] use pytorch to realize image style migration based on neural network
SQL head injection -- injection principle and essence
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
Customize the web service configuration file
【二叉树】删点成林
@What happens if bean and @component are used on the same class?
平安证券手机行开户安全吗?
【统计学习方法】学习笔记——支持向量机(下)
数据库系统原理与应用教程(008)—— 数据库相关概念练习题
浅谈估值模型 (二): PE指标II——PE Band