The Java array determines the length of the array when it is created. Index subscript starting from 0.
1. Array definition and initialization
int[] anarray;//Definition Anarray = new int[2];//initialization anarray[0] = 100;//Assignment anarray[1] = 200;//Assignment System.out.println ("Element at IND Ex 0: "+ anarray[0]);//Use SYSTEM.OUT.PRINTLN (" Element at index 1: "+ anarray[1]);//Use
Program output:
Element at index 0:100element at index 1:200
2. Define initialization and assign values at the same time
Int[] Anarray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
3. Multidimensional arrays
String[][] names = {"Mr.", "Mrs.", "Ms."}, {"Smith", "Jones"}; System.out.println (Names[0][0] + names[1][0]); System.out.println (Names[0][2] + names[1][1]);
Program output:
Mr smithms. Jones
As can be seen from the above example, the length of each row array in a Java multidimensional array does not require equality.
4. Copying an array
Class Arraycopydemo {public static void main (string[] args) {char[] CopyFrom = {' d ', ' e ', ' C ', ' A ', ' f ', ' F ', ' E ', ' I ', ' n ', ' a ', ' t ', ' e ', ' d '}; char[] CopyTo = new CHAR[7]; System.arraycopy (CopyFrom, 2, CopyTo, 0, 7); System.out.println (New String (CopyTo)); }}
Program output:
Caffein
Learn java-chapter 1 variable (Variables)-Array (Arrays)