当前位置:网站首页>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);
边栏推荐
- Solutions to cross domain problems
- 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)
- 2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
- wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
- [deep learning] image multi label classification task, Baidu paddleclas
- Inverted index of ES underlying principle
- Aike AI frontier promotion (7.7)
- 【二叉树】删点成林
- Upgrade from a tool to a solution, and the new site with praise points to new value
- Cookie
猜你喜欢
【深度学习】图像多标签分类任务,百度PaddleClas
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Tutorial on the principle and application of database system (011) -- relational database
Upgrade from a tool to a solution, and the new site with praise points to new value
数据库安全的重要性
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
JS to convert array to tree data
EPP+DIS学习之路(1)——Hello world!
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
随机推荐
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
解密GD32 MCU产品家族,开发板该怎么选?
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
Processing strategy of message queue message loss and repeated message sending
消息队列消息丢失和消息重复发送的处理策略
Cenos openssh upgrade to version 8.4
利用栈来实现二进制转化为十进制
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
@What happens if bean and @component are used on the same class?
Typescript interface inheritance
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
[pytorch practice] image description -- let neural network read pictures and tell stories
爱可可AI前沿推介(7.7)
【统计学习方法】学习笔记——支持向量机(上)
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
SQL blind injection (WEB penetration)
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
ps链接图层的使用方法和快捷键,ps图层链接怎么做的