Use of arraylist of Collection class

Source: Internet
Author: User

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 ===

Related Article

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.