Java array of objects

One-dimensional array:

1, the basic elements of the data type

2, a reference data element type, also known as an array of objects, i.e., objects are elements of the array

Note: An array of objects, we must first create an array object itself, that is, to determine the length of the array, and then create every element of the object, if not created, the default value is null elements of the array, so it is prone to a null pointer exception NullPointerException.

Sample code:

MyDate {class
    int year;
    int month The;
    int Day;
}
class the Test {
    public static void main (String [] args) {
        MyDate [] = new new MyDate ARR [. 3]; // create an array object itself, the specified length of the array
        
        for (int I = 0; I <arr.length; I ++) {
            ARR [I] = new new MyDate (); // every element of an object to be created
            ARR [I] = 1990 + I .year;
            ARR [I] .month . 1 + I =;
            ARR [I] .DAY,. 1 + = I;
        }
    }
}
 

Guess you like

Origin blog.csdn.net/Brevity6/article/details/90676723
Recommended