Java Class Object array declaration and initialization, java Array

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.