(1) definition of an array
An array is a data structure that is a collection that has the same type.
(2) syntax
There are three ways to define an array in Java:
① data type [] array name ={according to type initialization};
Int[] str1={1,2,3,3,4}; This method is initialized directly at the time of definition and, of course, determines its space size.
② data type [] array name =new data type [array space size];
int[] str2=new int[100];
Note: space size must be indicated here
③int[] str3=new []{1,2,3,4,5}; This method, like the first one, is initialized directly.
Assigning a value to an array
Assign a value to it by an array subscript: str2[0]=1;
Note: array subscript starts from 0;
Log Group Access
Like assignment, access is also done by array subscript
We can directly use one of these values int a= str1[0];
Logarithmic group traversal
(1) using a For loop
For (int I=0;i<str1.length;i++) {code block}
(2) There is a special method in Java
Foreach
For example: for (int i:str1) {System.out.println (i);}
Note: will traverse from head to tail
This article is from the "popular tastes" blog, make sure to keep this source http://asalawys.blog.51cto.com/12008007/1863184
An array of preliminary Java learning