First, array initialization
An array is the same type of an object sequence or primitive type data sequence that is encapsulated together with an identifier name. The compiler is not allowed to specify the length of the array, when using the statement int[] A, only have a symbol name, that is, a reference to an array, do not have the storage space for this array. To get the storage space of an array, you must write the initialization expression, the simplest type of expression as follows
1 int[] A = {n/a};
If you cannot determine the length of an array while writing a program, you can create an element in the array directly with new using the following method
1 int[] a = new int[3];
When an array of objects is created, the elements in the array are merely references to the objects, and when an attempt is made to use a null reference in the array, a compiler run-time error is raised, which is to be noted by using a list of curly braces to initialize an array of objects.
1 integer[] = {2 new Integer (1),3 new Integer (2),4 3, 5 }
There are two more ways to use array types, one is the length () method, which returns the lengths of the array, and the. toString () method, which prints the array in a visual way.
Second, variable parameter list
Let's give an example of a mutable parameter.
1 classa{2 3 }4 5 Public classtest{6 Static voidprint (Object ... args) {7 for(Object Obj:args) {8System.out.print (obj + "");9 }Ten System.out.println (); One } A Public Static voidMain (String args[]) { -PrintNewInteger (1),NewFloat (2.0),NewDouble (3.0)); -Print ("A", "B", "C"); thePrintNewA (),NewA (),NewA ()); - } - -}
1 1 2.0 3.0 2 a b C 3 [email protected] [email protected] [email protected ]
The effect of this approach is the harmonious coexistence of the variable parameter list and the automatic wrapper mechanism, using a variable parameter list, when the parameter type is specified, the compiler will give you an auto-populated array, which allows a set of things to be passed as a list to an array, and it is possible to pass the empty argument list to the mutable parameter list. In addition to the argument list of type object, the parameter list can use any type of parameter, including the base type.
Using a mutable parameter list can complicate the overloaded process, and you need to be aware of this complex problem, and here's an example.
1 Public classtest{2 Static voidf (Character ... args) {3System.out.print ("First");4 for(Character C:args) {5System.out.print (c + ""));6 }7 System.out.println ();8 }9 Ten Static voidf (Integer ... args) { OneSystem.out.print ("First"); A for(Integer C:args) { -System.out.print (c + "")); - } the System.out.println (); - } - - Static voidf (Long ... args) { +System.out.print ("Third"); - } + A Public Static voidMain (String args[]) { atF (' A ', ' B ', ' C '); -F (1); -F (2, 1); -F (0); -F (0l); - //f (); in } - to}
In this case, the F () method cannot be called because the compiler will not know which method to call, which is a potential risk to the mutable parameter list. To address this vulnerability, you can add a non-mutable parameter to each parameter list, for example:
1 static void f (Integer i,integer ... args); 2 static void f (float i, float... args);
Chapter5_ initialization and Cleanup _ array initialization and variable parameter list