Java Class Object array declaration and initialization, java Array
Java is a pure object-oriented language. Class is an important component. Then, in actual programming, We will customize some classes, such as Point
<span style="font-size:14px;">public class Point {public Point(){Lat=0.0;Lng=0.0;}public double GetLat(){return Lat;}public double GetLng(){return Lng;}public void SetLat(double pLat){Lat=pLat;}public void SetLng(double pLng){Lng=pLng;}private double Lat;private double Lng;}</span>
Such as object classes, we often need to use their object arrays. However, the object array declaration and initialization in Java are different from their fixed types of declarations and initialization. See below.
<span style="font-size:14px;">//Init class Pointpoints=new Point[totalPoints];//statement of points Array//Initing for(int i=0;i<totalPoints;i++){points[i]=new Point();}</span>
How to create and initialize an array in java?
========================================================== ==============
Define and initialize method 1:
String [] a = new String [3];
A [0] = "";
A [1] = "B ";
A [2] = "c ";
Define and initialize method 2:
String [] B = new String [] {"a", "B", "c "};
========================================================== ==============
How to initialize an object array in JAVA?
If you try to declare an object array, there is no example, so a null pointer exception will be reported.
This array object is currently initialized
Animals [] an = new Animals [5]; // This is only the declaration of an array of object types.
Required
For (int I = 0; I <5; I ++)
An [I] = new Animals ();
Now, you understand.
The one in front of you indicates the array, but does not call the Animals constructor. every element in your array is an object and must be instantiated before use.
If you only enter the user length,
Animals [] an = new Animals [n];
Variables can be used for declaration.
Or you can directly define an Animals [] an = new Animals [100]; define a large array, and then use new Animals (); For instantiation, you can also use the dynamic list <Animals> an = new external list <Animals> (); to define a dynamic array.