Simple learning of the arraylist class in Java Collection

Source: Internet
Author: User

I haven't written anything for a long time. I want to develop a good habit of writing something, because good memory is better than writing something. Now I feel that I have forgotten something I learned before.

Well, let's not talk about it. First, record what we did in February, and then start the text. I wrote a paper in early January and submitted it for external review. Later, I waited for the results and got a preliminary understanding of the python syntax. Because I didn't record it at the time and didn't actually do the exercises, I feel like I didn't learn it now, then I went home from the holiday.

I have little knowledge about Java collections and generics. Now I will take some notes. Java collections can be similar to C ++ containers and have the function of dynamically adding, deleting, modifying, and querying arrays. Of course, Java has many collection classes, such as arraylist, vector, stack, hashmap, and hashtable, the following describes arraylist. Other functions are similar in usage. You can view the JDK help documentation.

 

Package sort; import Java. util. arraylist; import Java. io. *; public class Array {public static void main (string [] ARGs) throws ioexception {clerkmana CM = new clerkmana (); bufferedreader BR = new bufferedreader (New inputstreamreader (system. in); While (true) {system. out. println ("1: add an employee"); system. out. println ("2: Find an employee"); system. out. println ("3: modifying an employee"); system. out. println ("4: deleting an employee"); system. out. println ("5: exit system"); string quit = BR. readline (); If (response. equals ("1") {system. out. print ("Enter the name:"); string name = BR. readline (); system. out. print ("Enter age:"); int age = integer. parseint (BR. readline (); system. out. print ("Enter the salary:"); float sale = float. parsefloat (BR. readline (); clerk EMP = new clerk (name, age, sale); cm. addclerk (EMP);} else if (response. equals ("2") {system. out. print ("Enter the name:"); string name = BR. readline (); cm. showinfo (name);} else if (response. equals ("3") {} else if (response. equals ("4") {} else {// system. exit (0); break ;}}} class clerkmana {private arraylist Al = NULL; Public clerkmana () {Al = new arraylist ();} public void addclerk (clercle) {Al. add (CLE);} public void showinfo (string name) {for (INT I = 0; I <Al. size (); I ++) {clerk temp = (clerk) Al. get (I); If (temp. getname (). equals (name) {system. out. println (temp. getname () + "," + temp. getage () ;}} public void showall () {for (INT I = 0; I <Al. size (); I ++) {clerk temp = (clerk) Al. get (I); system. out. println (temp. getname () + "," + temp. getage () ;}} public void editclerk (string name, int newage) {for (INT I = 0; I <Al. size (); I ++) {clerk temp = (clerk) Al. get (I); If (temp. getname (). equals (name) {temp. setage (newage) ;}}public void delclerk (string name) {for (INT I = 0; I <Al. size (); I ++) {clerk temp = (clerk) Al. get (I); If (temp. getname (). equals (name) {Al. remove (I) ;}}} class clerk {private string name; Public String getname () {return name;} public void setname (string name) {This. name = Name;} private int age; 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;} private float Sal; Public Clerk (string name, int age, float Sal) {This. name = Name; this. age = age; this. sal = Sal ;}}

The modification and deletion functions are not implemented. This article mainly lists its usage.
 

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.