Java-collection framework-ArrayList from scratch in Android Development

Source: Internet
Author: User

[Java] package test; import java. io. bufferedReader; import java. io. inputStreamReader; import java. util. *;/** Author: Zhong Zhigang * function: ArrayList of Collection framework * Time: * 1. List structure: ArrayList, sorted List, Vector, Stack; * 2. Map Structure: hashMap, Hashtable; * 3, set structure: HastSet, TreeSet; * 4, Queue structure: queue interface */public class Collection framework {/*** @ param args */public static void main (String [] args) throws Exception {// ArrayList <Clerk> al = new ArrayList <Clerk> (); clerc1 = new Clerk ("", 50,100 0); al. add (c1); // add to the end Clerk c3 = new Clerk ("Wu Yong", 45,200 0); al. add (c3); // add it to the end of al. add (c3); // you can add the same object System. out. println ("al. lenght = "+ al. size (); Clerk c2 = new Clerk ("Wu Song", 30,100 04.4f); al. add (0, c2); // add it to the specified location. The original location will be shown as "al. remove (3); // Delete the corresponding object // System. out. println (al. get (1 ). getName (); for (Clerc: al) {// System. out. println (c. GetName ();} // use a class to manage employee information. EmpManage emp = new EmpManage (); // read the input BufferedReader br = new BufferedReader (new InputStreamReader (System. in); while (true) {System. out. println ("follow the prompts"); System. out. println ("1: add an employee"); System. out. println ("2: Find an employee"); System. out. println ("3: modifying an employee's salary"); System. out. println ("4: deleting an employee"); System. out. println ("6: Find all employees"); System. out. println ("5: Exit program"); String op = br. read Line (); if (op. equals ("1") {System. out. println ("Enter the name:"); String name = br. readLine (); System. out. println ("Enter age:"); int age = Integer. valueOf (br. readLine (); System. out. println ("Enter the salary:"); float sal = Float. parseFloat (br. readLine (); Clerk c = new Clerk (name, age, sal); emp. addEmp (c); // System. out. println ("successfully added");} else if (op. equals ("2") {System. out. println ("Enter employee name"); String qname = br. readLine (); Emp.info (qname);} else if (op. equals ("3") {System. out. println ("enter the name of the employee to be modified:"); String uname = br. readLine (); System. out. println ("Enter the salary to be modified:"); float newsal = Float. parseFloat (br. readLine (); emp. updateSal (uname, newsal); // System. out. println ("modified successfully");} else if (op. equals ("4") {System. out. println ("enter the name of the employee to be deleted:"); String dname = br. readLine (); emp. delEmp (dname); // System. out. println ("deleted successfully");} else If (op. equals ("5") {System. exit (0);} else if (op. equals ("6") {emp. queal () ;}// queue list. serializable linked list // sorted list ll = new employee list () ;}} class EmpManage {// employee management class private ArrayList <Clerk> al = null; public EmpManage () {al = new ArrayList <Clerk> ();} public void queal () {for (Clerk c: al) {System. out. println ("employee name:" + c. getName () + ", salary:" + c. getSal () + ", age:" + c. getAge () ;}// add employee public void DdEmp (Clerc) {al. add (c) ;}// Delete employee public void delEmp (String name) {boolean B = false; for (Clerc: al) {if (c. getName (). equals (name) {// String type data is compared with the address al with =. remove (c); B = true; break;} if (! B) {System. out. println ("");} else {System. out. println ("deleted successfully") ;}// display employee information public void info (String name) {boolean B = false; for (int I = 0; I <al. size (); I ++) {Clerc = al. get (I); if (c. getName (). equals (name) {B = true; System. out. println ("your employee:" + c. getName () + ", salary:" + c. getSal () + ", age:" + c. getAge (); break ;}} if (! B) {System. out. println ("");} else {System. out. println ("query succeeded") ;}// modify employee salary public void updateSal (String name, float sal) {boolean B = false; for (Clerc: al) {if (c. getName (). equals (name) {c. setSal (sal); B = true ;}} if (! B) {System. out. println ("");} else {System. out. println ("modified successfully") ;}} class Clerk {// employee class private String name; public String getName () {return 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;} private int age; private float sal; public Clerk (String name, int age, float sal) {this. age = age; this. name = name; this. sal = sal ;}}

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.