当前位置:网站首页>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);
边栏推荐
- NGUI-UILabel
- [Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
- File upload vulnerability - upload labs (1~2)
- Hi3516 full system type burning tutorial
- 利用棧來實現二進制轉化為十進制
- [statistical learning methods] learning notes - Chapter 5: Decision Tree
- Aike AI frontier promotion (7.7)
- AirServer自动接收多画面投屏或者跨设备投屏
- SQL blind injection (WEB penetration)
- idm服务器响应显示您没有权限下载解决教程
猜你喜欢

全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功

SQL head injection -- injection principle and essence

2022广东省安全员A证第三批(主要负责人)考试练习题及模拟考试

The road to success in R & D efficiency of 1000 person Internet companies

Vxlan 静态集中网关

The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful

SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels

The left-hand side of an assignment expression may not be an optional property access.ts(2779)

ps链接图层的使用方法和快捷键,ps图层链接怎么做的

wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
随机推荐
Epp+dis learning path (1) -- Hello world!
【PyTorch实战】图像描述——让神经网络看图讲故事
Vxlan 静态集中网关
数据库系统原理与应用教程(008)—— 数据库相关概念练习题
Zhimei creative website exercise
解密GD32 MCU产品家族,开发板该怎么选?
【PyTorch实战】用RNN写诗
Experiment with a web server that configures its own content
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
广州市召开安全生产工作会议
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
File upload vulnerability - upload labs (1~2)
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
Minimalist movie website
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
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)
Attack and defense world - PWN learning notes
Cookie
SQL blind injection (WEB penetration)