What is an array: An array is one of the most important data structures for every programming language, and the implementation and processing of arrays of different languages are not identical.
Java array: Used to store the same type elements of a fixed size.
A declaration, creation, initialization of a Java array
Write in front: Defining a Java array includes declaration and creation: it can be declared, then created (two steps), or it can be declared and created at the same time (one step).
1. Disclaimer
Method 1 (preferred):dataType[] arrayname; Example: string[] country;
Method 2:dataType arrayname[];: example: String country[];
< Span class= "hl-brackets" > 2. Create
< Span class= "hl-brackets" > method: Arrayname = new Datatype[arraysize];&NBSP;
Explanation: An array was created using datatype[arraysize]. Assigns a reference to the newly created array to the variable arrayname.
Example: Country=new string[4]; Creates an array with a type of String, an array length of 4, and assigns a reference to the array to country;
3. Simultaneous declaration and creation
Method:dataType[] arrayname= new dataType[arraySize]; Example: string[] Country=new STRING[4];
4. Initializing an array
Method 1:datatype[] arrayname= new type[]{value1,value2,value3,......}; The red part can be saved slightly, so there are two kinds:
Example 1:string[] Country = {"China", "America", "England", "Japan"}
Example 2:string[] Country = new String[]{"China", "America", "England", "Japan"}
The array length cannot be added to [] in Example 2, because the number of elements is determined by the contents of the following curly braces.
Method 2: Create the array first, then assign the values separately, i.e. DataType[] arrayname= new dataType[arraySize];arrayname[0 ]=value1;......arrayname[arraysize-1]=value;
Example: string[] country=new string[4]; Country[0]= "China"; Country[1= "America"; country[2]= "England"; country[3]= "Japan";
Note: The subscript of an array is counted starting at 0, i.e. Arrayname[0] is the first value of the array
Common methods of two arrays
1. Traverse
Method One: For loop, example below
Package Bokeyuan;
public class Arraytest {
public static void Main (string[] args) {
/* Example 1*/
string[] Country = new string[]{"China", "America", "England", "Japan"};
for (int i=0;i<country.length;i++) {
System.out.print (country[i]+ ""); Output: China America England Japan
}
/* Example 2*/
Int[] MyNum = {1,2,3,4,5,6,7,8,9,10};
int numcount=0;
for (int i=0;i<mynum.length;i++) {
Numcount+=mynum[i];
System.out.print (mynum[i]+ ""); Output results: 1 2 3 4 5 6 7 8 9 10
}
System.out.println (Numcount); Output results: 55
}
}
Method 2:foreach traversal, as shown below
/*Example 1*/string[] Country=Newstring[]{"China", "America", "England", "Japan"}; for(String element:country) {System.out.print (element+" ");//output: China America England Japan } /*Example 2*/ int[] MyNum = {1,2,3,4,5,6,7,8,9,10}; intNumcount=0; for(intNum:mynum) {//where num is a temporary variable that is used to receive data and can be written as any other valid identifier; Aaa,elenumcount+=num; System.out.print (Num+" ");//Output Results: 1 2 3 4 5 6 7 8 9} System.out.println (Numcount); //Output Result:
An analysis of the Java array-Basic explanation