Learning notes on Java programming ideas

5.8 array initialization

An array is a sequence of objects or basic data types of the same type and encapsulated together with an identifier name. Arrays are manipulated and used by square bracket subscript operators.

//Two ways to define arrays,Generally, the first one is used
int[] a1;
int a2[];

The compiler does not allow you to specify the size of the array, because what is specified now is only a reference to the array, and no space has been allocated for the array object itself. In order to create the corresponding storage space for the array, you must initialize the expression. Array initialization can occur anywhere in the code. Of course, it can also be initialized with curly braces immediately after definition. In this way, the compiler will be responsible for the allocation of storage space (equivalent to new).

It should be noted that what is defined here is only the reference of the array. If one array is directly assigned to another, in fact, only the reference is assigned, which is consistent with the alias mentioned earlier.

C and C + + can allow out of bounds when using arrays, but Java will directly encounter runtime errors.

As mentioned earlier, the array should be initialized before use after it is defined. If you don't know how many elements will appear in the array, you can use random numbers to decide.

5.8.1 list of variable parameters:

All classes in Java are directly or briefly inherited from the Object class. Applying this feature can solve the problem of creating methods that do not know the number and type of parameters temporarily.

public class VarArgs {
    static void printArray(Object[] args){
        for (Object obj : args)
            System.out.println(obj+" ");
        System.out.println();
    }
    public static void main(String[] args){
        printArray(new Object[]{new Integer(47),new Float(0.23),new Double(3.145)});
        printArray(new Object[]{"one","two","three"});
        printArray(new Object[]{new A(),new A(),new A()});
    }
}
class A{}
//        47
//        0.23
//        3.145
//
//        one
//        two
//        three
//
//        A@1b6d3586
//        A@4554617c
//        A@74a14482 

The above example is to use the array parameters of Object type to realize the variable parameter list, which can deal with various types and arbitrary length parameter lists, but Java # SE5 provides a simpler writing method:

public class NewVarArgs {
    static void printArray(Object... args){
        for(Object obj : args)
            System.out.println(obj + " ");
        System.out.println();
    }
    public static void main(String[] args){
        //Directly accept parameters without using an array
        printArray(new Integer(47),new Float(1.23),new Double(3.1415));
        printArray("one","two","three");
        printArray(new B(),new B(),new B());
        //You can also accept array parameters
        printArray((Object[])new Integer[]{1,2,3,4});//This involves explicit type conversion
        printArray();//No problem with empty parameters
    }
}
class B{}
//        47
//        1.23
//        3.1415
//
//        one
//        two
//        three
//
//        B@1b6d3586
//        B@4554617c
//        B@74a14482
//
//        1
//        2
//        3
//        4 

With this three-point writing method, we no longer need to write an Object array. In fact, this work is completed by the compiler instead of us. From the internal foreach syntax, we can see that the method still receives an array type.

public class OptionalTrailingArguments {
    static void f(int required, String... trailing){
        System.out.println("required:" + required);
        for(String str : trailing){
            System.out.println(str + " ");
        }
        System.out.println();
    }
    public static void main(String[] args){
        f(1,"one");
        f(2,"one","two");
        f(0);
    }
}
//        required:1
//        one
//
//        required:2
//        one
//        two
//
//        required:0

This example shows that the variable parameter list can also accept parameters of a specified type, or add a parameter number to determine whether it is equal to the number of accepted parameters. You can also use args Statements such as getClass () perform more stringent type checking.

5.9 enumeration types

enum keyword is a newly added feature of Java} SE5, which makes it easy for us to handle when we need groups and use enumerated type sets. Before that, you need to create an integer constant set, but these enumerated values do not necessarily limit their own values to the range of the integer constant set, which has the risk of crossing the boundary.

public enum Spiciness{
        NOT,MILD,MEDIUM,HOT,FLAMING
    }

The above example creates an enumeration type named Spiciness, which has five named values and naming conventions. The enumeration type is all capitalized and underlined to separate multiple words.

public static void main(String[] args){
        Spiciness howHot = Spiciness.FLAMING;
        System.out.println(howHot);
    }
//output:
//FLAMING

The above example is the use method of enumeration type. Before using enum, you need to create a reference of this type and assign it to an instance.

Another useful feature of enum is that it can be used in switch statements.

 

2.17 upload # Chapter 5 to finish reading

P142

 

Tags: Java

Posted by dipenmistry on Mon, 18 Apr 2022 18:03:34 +0930