当前位置:网站首页>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 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
4. Array declaration
① 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}
};
② 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 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);
边栏推荐
- SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
- GCC compilation error
- NGUI-UILabel
- 静态Vxlan 配置
- 利用棧來實現二進制轉化為十進制
- 利用栈来实现二进制转化为十进制
- 2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试
- OSPF exercise Report
- [Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
- 金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
猜你喜欢
leetcode刷题:二叉树19(合并二叉树)
Idea 2021 Chinese garbled code
Epp+dis learning road (2) -- blink! twinkle!
VSCode的学习使用
Decrypt gd32 MCU product family, how to choose the development board?
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
Experiment with a web server that configures its own content
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
Zhimei creative website exercise
IPv6 experiment
随机推荐
gcc 编译报错
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
Cenos openssh upgrade to version 8.4
leetcode刷题:二叉树24(二叉树的最近公共祖先)
【深度学习】图像多标签分类任务,百度PaddleClas
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
编译 libssl 报错
Several methods of checking JS to judge empty objects
Aike AI frontier promotion (7.7)
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
【统计学习方法】学习笔记——支持向量机(上)
Vxlan 静态集中网关
EPP+DIS学习之路(2)——Blink!闪烁!
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
Customize the web service configuration file
免备案服务器会影响网站排名和权重吗?
Tutorial on principles and applications of database system (009) -- conceptual model and data model
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
AirServer自动接收多画面投屏或者跨设备投屏