1.ArrayList is a generic class that takes a type parameter.
2. In order to specify the element object type that the array list holds, you need to enclose the class name with a pair of angle brackets to add to the following.
arrylist<a> s = new arraylist<a> ();//The argument in the right angle bracket can be omitted
3. Add elements to the array list using the Add method.
S.add (A ());
4. The array list manages an internal array of object references. If add is called and the internal array is full, the array list automatically creates a larger array and copies all the objects from the smaller array to the larger array.
5. Can also pass the accident capacity to the ArrayList constructor
arraylist<a> s = new arraylist<> (100);
The 6.size method returns the number of actual elements contained in the array list.
7. Once you are able to confirm that the size of the array is not changing, you can call the TrimToSize method.
The 8.ArrayList class is not a part of the Java programming language, it is just a utility class written by someone and placed in a standard library.
9. Use the Get and set methods to implement an operation that accesses or alters an array element.
S.set (i,b);//Convert the first element in s to ba C = s.get (i); Returns the first element of S, saved in C
10. Use the ToArray method to copy the array elements into an array.
a[] n = new A[s.size ()];s.toarray (a);
11. In addition to appending elements to the end of an array list, you can also insert elements in the middle of an array list, using the Add method with indexed parameters.
int n = s.size ()/2;s.add (n,d);
12. Remove the element using the Remove method.
S.remove (n);
Instance Code
Test class
Import java.util.*;p ublic class Test {public static void main (string[] args) {arraylist<employee> SF = new arraylist& Lt;> (); Sf.add (New Employee ("W1", 1000,1990,1,1)), Sf.add (New Employee ("W2", 1500,1988,1,1)), Sf.add (New Employee (" W3 ", 2000,1989,1,1)); for (employee E:SF) e.raisesalary (5); for (employee E:SF) System.out.println (" name = "+ e.getname () + ", salary =" + e.getsalary () + ", Hireday =" +e.gethireday ());} }
Employee Class
Import Java.util.date;import Java.util.gregoriancalendar;public class Employee {private String name;private Date Hireday;private double salary;public Employee (String n,double s,int year,int month,int day) {name = N;salary = S; GregorianCalendar gr = new GregorianCalendar (year,month-1,day); hireday = Gr.gettime ();} Public String GetName () {return name;} Public double getsalary () {return salary;} Public Date Gethireday () {return hireday;} public void Raisesalary (double p) {Double raise = salary * p/100;salary + = Raise;}}
My Java Learning Note (10) about the generic data list