An array is a data structure that stores the same type
Declaration and initialization of arrays
int [] A; int a[]; int New int [100];
int New int [+]; for (int i = 0; i <; i++) // fills the array with numbers 0 to
Cannot change its size once it is created
3.10.1 for Each loop
The For Each loop can be used to assign a value to an array
for (variable:collection) statement
Int[] A = new int[100];
for (int element:a) System.out.println (element);
Output for each element of array A.
If you want to output an array directly, you can use the ToString method of the Arrays (Java.util.Arrays) class.
System.out.println (Arrays.tostring (a));
3.10.2 array initialization with anonymous array
int [] Smallprimes = {2, 3, 5, 7, one, one }; anonymous array initialization:newint[] {n, a,at EWint[] {n, a, a, A, A, a PNS}; is an int[] anonymous = {: A shorthand for 10> anonymous;
3.10.3 Copying an array
int [] copiedluckynumbers = arrays.copyof (luckynumbers, luckynumbers.length);
If the length exceeds the value, the assignment is 0.
3.10.4 command-line arguments
If you execute a program using the command Java message-g Cruel World
Then you can accept it in the main function.
Args[0]: "-G"args[1]: "Cruel"args[2]: "World"
Public classmessage{ Public Static voidMain (string[] args) {if(Args.length = = 0 | | args[0].equals ("-H"))) System.out.print ("Hello,"); Else if(Args[0].equals ("-G")) System.out.print ("Goodbye,"); //Print the other command-line arguments for(inti = 1; i < args.length; i++) System.out.print (" " +Args[i]); System.out.println ("!"); }}
3.10.5 Array Sorting
int New int [10000]; Arrays.sort (a)
3.10.6 Multi-dimensional arrays
Double [] balances;
int [] Magicsquare ={ {3, 2, +}, {5, ten, one, 8}, {9, 6, 7,}, {4 , 1}};
for (double[] row:a) for (double value:row) do something with value
System.out.println (Arrays.deeptostring (a)); Fast output 2 multi-dimensional arrays
3.10.7 Irregular Array (ragged Arrays)
Java does not really have a strict multidimensional array, he is an array of array storage, each one-dimensional array of elements can be different, the type can be different.
COREJAVAE10V1P3.10 3rd Java BASIC Programming Structure-3.10 Array (Arrays)