当前位置:网站首页>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 :

边栏推荐
猜你喜欢

AQS implementation principle

JSP built-in object (implicit object) -- input / output object

JSP implicit object -- scope

The idea of stack simulating queue

Ethernet switching security

Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合

Spotty music data client_ ID account

动态性能视图概述

Vscode cannot start the problem solving idea

Table fix specific rows
随机推荐
IDEA settings设置快捷键实现字符串中的英文字母转大小写
Lambda and stream
Burp Suite-第二章 Burp Suite代理和浏览器设置
给项目日志加上traceid
Why don't you tell me what long polling is?
2022.7.22DAY612
一点一点理解微服务
Spotty music data client_ ID account
爬虫->TpImgspider
JWT quick start
Burp Suite-第一章 Burp Suite 安装和环境配置
JSP built-in object (implicit object)
Jmeter性能测试之使用存储响应内容到文件监听器
System architecture & microservices
Rack server expansion memory
What is message subscription and publishing?
Use of JMeter performance test to store response content to file listener
Summary of common methods of string
How WPS sets page headers page by page
Burp Suite-第五章 如何使用Burp Target