当前位置:网站首页>Introduction to arrays -- array
Introduction to arrays -- array
2022-07-26 08:04:00 【Start】
Java Array

Arrays are one of the most important data structures for every programming language , Of course, the implementation and processing of arrays are different in different languages .
Java The array provided in the language is used to store fixed size elements of the same type .
You can declare an array variable , Such as num[10] Instead of a direct statement 10 Independent variables num0,num1,....,num10.
Declare array variables
data type [] Array name ; perhaps data type Array name [];
int[] nums1;
or
int nums1[];
Be careful : It is recommended to use int [] nums1; Declare array variables in a declaration style . int nums1[]; Style comes from C/C++ Language , stay Java In order to make C/C++ Programmers can quickly understand java Language .
Create array
Java Language use new To create an array , The grammar is as follows :
Declare a length of 5 Of int An array of types
int[] nums1 = new int[3];
Assign value to array
Array name [ Subscript ] = data ;
int[0]=1;
int[1]=2;
int[2]=3;
Operational data , Use array names [ Array element subscript ] To manipulate data
Change the first element of the array into 1000:
nums1[0] = 1000;
System.out.println(nums1[0]);
java There is 8 Array of large basic types
Each has its own default value :
byte[] nums1;// The default value is 0
short[] nums2;// The default value is 0
int[] nums3;// The default value is 0
long[] nums4;// The default value is 0
float[] nums5;// The default value is 0.0
double[] nums6;// The default value is 0.0
char[] chars;// The default value is : Space
boolean[] bools;// The default value is false
java Nissan definition arrays are mostly abbreviated
- data type [] Array name = new data type []{ data 1, data 2,...., data n};
- data type [] Array name = { data 1, data 2,...., data n};
// Declare a length of 5 Of int Type array , The data inside are 11 25 36 98 75
int[] nums = new int[]{11,25,36,98,75};
// Declare a length of 5 Of char An array of types , The elements inside are 'a' 'f' '3' 'm' 'r'
char[] chars = {'a','f','3','m','r'};
Traversal of array
Every element in the array can be accessed by index . The index of the array is usually from 0 Start , That is, the index of the first element in the array is 0. The last element of the array is located in (n-1) An index . We call it based on 0 The index of . Arrays can also be based on other numbers , We call it based on n The index of .
Use for Simply loop through all indexes in the array , You can access all the elements in the array .
public static void main(String[] args) {
// Define a sum Array
int[] sum={1,5,6,8};
// Traverse
for (int i = 0; i < sum.length; i++) {
System.out.print(sum[i]+" ");
}
}Running results :

For-Each loop
JDK 1.5 Introduced a new type of cycle , go by the name of For-Each Cycle or enhanced cycle , It can traverse arrays without subscripts .
The syntax is as follows :
for(type element: array)
{
System.out.println(element);
} public static void main(String[] args) {
// Define a sum Array
int[] sum={1,5,6,8};
// Traverse
/*for (int i = 0; i < sum.length; i++) {
System.out.print(sum[i]+" ");
}*/
for (int i : sum) {
System.out.print(i+" ");
}
}
The result is the same :

边栏推荐
- 咱就是来聊聊并发编程的三大核心问题。
- 爬虫->TpImgspider
- 一点一点理解微服务
- Stm8 official library file download
- Ethernet switching security
- Spotty music data client_ ID account
- Add traceid to the project log
- Polymorphism, final and interface
- Lnmp+wordpress to quickly build a personal website
- Now developers are beginning to do testing. Will there be no software testers in the future?
猜你喜欢

要不你给我说说什么是长轮询吧?

Spotty music data client_ ID account
![[uniapp] encapsulation of multiple payment methods](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[uniapp] encapsulation of multiple payment methods

Rack server expansion memory

Master slave database deployment

Ten thousand words long article | deeply understand the architecture principle of openfeign

The analysis, solution and development of the problem of router dropping frequently

Crawler - > tpimgspider

【 fastjson1.2.24反序列化漏洞原理代码分析】

Excel file reading and writing (creation and parsing)
随机推荐
Basic introduction of JDBC
The analysis, solution and development of the problem of router dropping frequently
Abstract classes and interfaces
shardingjdbc踩坑记录
Ten thousand words long article | deeply understand the architecture principle of openfeign
Network ()
Sort sort IP addresses
Burp Suite-第七章 如何使用Burp Scanner
Hystrix配置简单说明
Excel file parsing
PyTorch
MySQL implementation plan
Use js to count the number of occurrences of each string in the string array, and format it into an object array.
Shardingsphere data slicing
Matlab-二/三维图上绘制黑点
Burp Suite-第四章 SSL和Proxy高级选项
Spotty music data client_ ID account
Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
Logical volume management (LVM)
Why don't you tell me what long polling is?