Introduction: the Java array can be stored either basic types of data, types of data may also store references, but requires all of the array elements have the same data type. In addition, Java array is a data type, which is itself a reference type.
First, the array definition:
Data Type [] array name; such as:
int [] arrTest;
Second, the array initialization:
1. Static Initialization:
// embodiment an int [] the intArr; intArr = new you [] {1,2,3,4 }; // Second way int [] A = {1,2,3,4};
2. Dynamic Initialization:
int[] prices = new int[5];
Third, the use of an array:
1. Output array elements:
System.out.println(intArr[3]);
2. traverse the array elements:
for(int i =0; i<intArr.length; i++){ System.out.println(intArr[i]); }
Four, foreach loop:
public class ForEachTest { public static void main(String[] args) { String[] names = {"Jerry", "Amy", "Luna"}; for(String name:names){ System.out.println(name); } } }
Output:
Jerry
Amy
Luna
When using the foreach loop, it does not change the value of the array elements, so do the foreach loop variable assignment.