First, introduce
1, the generic is what first tell everyone arraylist is generic type. That ArrayList can accomplish what unexpected function. Let's take a look at the following code:
[Java]View plain copy arraylist<string> strlist = new arraylist<string> (); arraylist<integer> intlist = new arraylist<integer> (); arraylist<double> doublelist = new arraylist<double> (); Everyone is familiar with the ArrayList, which constructs three lists, each with string, Integer, and double; This is the extraordinary thing about ArrayList: that all types of variables can be assembled into a corresponding list, Instead of implementing a build ArrayList class for each type. Here may not understand, the opening is always difficult, below see if there is no generics, how do we do;
2, no generics will doLet's look at the following code:
We implement two classes that can set the point coordinates, and set the point coordinates of the integer type and the point coordinates of the float type, respectively:
[Java] View plain copy//Set integer type point coordinates class integerpoint{ private Integer x ; // representing x coordinates private Integer y ; // Represents y-coordinate public void setx (integer x) { this.x = x ; } public void sety (integer y) { this.y = y ; } public integer getx () { return this.x ; } public Integer&nbsP;gety () { return this.y ; } } //Set point coordinates of float type class floatpoint{ private float x ; // Show X coordinates private Float y ; // represents y-coordinate public void setx (float x) { this.x = x ; } public void sety (float y) { this.y = y ; } public float getx () { return this.x ; } public float gety () { return this.y ; } } Now there is a question: Have you found that they except variable type, one is the integer one is float, the other is no different. Can we merge into one, then?
The answer is yes, because integer and float are derived from object, we use the following code instead:
[Java]View Plain