I have never created an object array in C # before. I wrote a small exercise today and it will not be used when I use an object array.
Creating an object array in C # is not like C ++.
Code:
View plaincopy to clipboardprint?
- Student [] student = new student [5];
Student [] student = new student [5];
Parameters are not specified with parentheses when an object array is created, unlike creating a single object.
Code for creating a single object:
View plaincopy to clipboardprint?
- Student = new student ();
Student = new student ();
At the same time, if the brackets in the preceding array dimensions cannot specify the dimension when creating an object array, if the dimension is specified, an error occurs during compilation:
Student [5] student = new student [5]; // It cannot be compiled here.
Note that after creating an object array, no object is generated, but an array referenced by the object is simple. If an object is used at this time, a null pointer reference will appear. When using an array for each reference, you also need to use new to create an object.
View plaincopy to clipboardprint?
- Student [] student = new student [5];
- Student [0] = new student ();
- Student [0]. Age = 12;
- Student [1] = new student ();
- Student [1]. Age = 13;
- Student [2] = new student ();
- Student [2]. Age = 74;
- Student [3] = new student ();
- Student [3]. Age = 34;
- Student [4] = new student ();
- Student [4]. Age = 32;
I have never created an object array in C # before. I wrote a small exercise today and it will not be used when I use an object array.
Creating an object array in C # is not like C ++.
Code:
View plaincopy to clipboardprint?
- Student [] student = new student [5];
Student [] student = new student [5];
Parameters are not specified with parentheses when an object array is created, unlike creating a single object.
Code for creating a single object:
View plaincopy to clipboardprint?
- Student = new student ();
Student = new student ();
At the same time, if the brackets in the preceding array dimensions cannot specify the dimension when creating an object array, if the dimension is specified, an error occurs during compilation:
Student [5] student = new student [5]; // It cannot be compiled here.
Note that after creating an object array, no object is generated, but an array referenced by the object is simple. If an object is used at this time, a null pointer reference will appear. When using an array for each reference, you also need to use new to create an object.
View plaincopy to clipboardprint?
- Student [] student = new student [5];
- Student [0] = new student ();
- Student [0]. Age = 12;
- Student [1] = new student ();
- Student [1]. Age = 13;
- Student [2] = new student ();
- Student [2]. Age = 74;
- Student [3] = new student ();
- Student [3]. Age = 34;
- Student [4] = new student ();
- Student [4]. Age = 32;