The array function is indeed very powerful and useful, but now another problem is that all employees of the company should be managed and the information should be added when new employees join, when someone leaves, the information will be deleted. That is to say, this is a dynamic management process. If an array is used, it is not known how much space should be opened for the array. If it is large, it will be a waste, but it will not be enough in the future, as a result, the dynamic features of the Collection class are used. The arraylist set management process is: the space is not used after the creation, however, every time an object is added, it adds the address reference of the object to the end of its memory space and numbers it in order. The program is as follows:
Package com. test;
Import java. util .*;
Public class test5 {
Public static void main (string [] ARGs ){
Arraylist Al = new arraylist ();
System. Out. println ("the size of Al is:" + Al. Size ());
Clerk clerk2 = new clerk ("wuyong );
Al. Add (clerk2 );
System. Out. println ("the size of Al is:" + Al. Size ());
For (INT I = 0; I <Al. Size (); I ++) {// access data in Al and forcibly convert the object into a clerk object
Clerk temp1 = (clerk) Al. Get (I );
System. Out. println (temp. getname ());
}
Clerk temp = (clerk) Al. Get (0 );
System. Out. println (temp. getname ());
Al. Remove (0 );
System. Out. println ("=== after deleting = === ");
For (INT I = 0; I <Al. Size (); I ++) {// traverses all objects
Clerk temp1 = (clerk) Al. Get (I );
System. Out. println (temp1.getname ());
}
}
}
Class clerk {
Private string name;
Private
Int age;
Private
Float Sal;
Public Clerk (string name, int age, float Sal ){
This. Name = Name;
This. Age = age;
This. sal = Sal;
}
Public String getname (){
Retrun name;
}
Public void setname (string name ){
This. Name = Name;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
Public float getsal (){
Return Sal;
}
Public void setsal (float Sal ){
This. sal = Sal;
}
}
Result: The size of Al is 0.
The size of Al is: 0.
Wuyong
Wuyong
=== After Song Jiang is deleted ===