Set Exercises and exercises

Source: Internet
Author: User

Set Exercises and exercises

Use ArrayList

1. Store multiple employee information, including employee ID, name, age, and start time. Print all employee names one by one and output the number of employees.

Package CollectionPart; import java. util. arrayList; import java. util. list; public class ArrayListPractise_1 {public static void main (String [] args) {Employee_1 e1 = new Employee_1 ("dept0000001", "lifei", 24, "2016-03-28 "); employee_1 e2 = new Employee_1 ("dept0000002", "li2fe", 23, "2015-01-22"); Employee_1 e3 = new Employee_1 ("dept%003", "lifei3", 28, "2016-02-05"); Employee_1 e4 = new Employee_1 ("dept2_001", "lif4i", 25, "2017-03-28"); List <Employee_1> myList1 = new ArrayList (); myList1.add (e1); myList1.add (e2); myList1.add (e3); myList1.add (e4); for (Employee_1 employee_1: myList1) {System. out. println (employee_1.getEmployeeName ();} System. out. println ("Total Number of employees:" + myList1.size ());}}
Package CollectionPart; public class Employee_1 {private String employeeId; private String employeeName; private int employeeAge; private String employeeHireDate;/* It is stored as String type and set to date format in the database. Then use Date today = new Date (); SimpleDateFormat fm = new SimpleDateFormat ("YYYY-MM-dd"); to do */public String getEmployeeId () {return employeeId ;} public void setEmployeeId (String employeeId) {this. employeeId = employeeId;} public String getEmployeeName () {return employeeName;} public void setEmployeeName (String employeeName) {this. employeeName = employeeName;} public int getEmployeeAge () {return employeeAge;} public void setEmployeeAge (int employeeAge) {this. employeeAge = employeeAge;} public String getEmployeeHireDate () {return employeeHireDate;} public void setEmployeeHireDate (String employeeHireDate) {this. employeeHireDate = employeeHireDate;} @ Override public String toString () {return "Employee_1 [employeeId =" + employeeId + ", employeeName =" + employeeName + ", employeeAge = "+ employeeAge +", employeeHireDate = "+ employeeHireDate +"] ";} public Employee_1 (String employeeId, String employeeName, int employeeAge, String employeeHireDate) {super (); this. employeeId = employeeId; this. employeeName = employeeName; this. employeeAge = employeeAge; this. employeeHireDate = employeeHireDate;} public Employee_1 () {super ();}}

 

Exercise 2:

This information is added when an employee joins the company and deleted when the employee leaves the company.

Sample Code: [if there is a bug, when you enter the date, you will not wait until the date is entered, and you will directly ask if there are new employees]

The following is a solution for solving ConcurrentModificationException. Is to get the subscript of the current element and then delete it. The following code does not write comments. If you have any questions, please discuss them.

Package CollectionPart; import java. util. arrayList; import java. util. list; import java. util. optional; public class ArrayListPractise_2 {public static void main (String [] args) {ArrayListPractise_2 ap2 = new ArrayListPractise_2 (); List <Employee_1> myList = new ArrayList (); System. out. println ("Are there employees? "); Rule in = new rule (System. in); String string = in. nextLine (); while (string. equals ("yes") {System. out. println ("Enter employee id"); String id = in. nextLine (); System. out. println ("Enter employee name"); String name = in. nextLine (); System. out. println ("Enter the employee's age"); int age = in. nextInt (); System. out. println ("Enter employee start time"); String hireDate = in. nextLine (); System. out. println ("? "); Employee_1 e1 = new Employee_1 (id, name, age, hireDate); myList. add (e1); System. out. println (" Are there new employees? "); String = in. nextLine ();} System. out. println ("Total employees:" + myList. size () + "persons"); List <Employee_1> newList = null; System. out. println ("Are there Employees leaving? "); String = in. nextLine (); while (string. equals ("yes") {System. out. println ("the employee's name is"); String name = in. nextLine (); newList = ap2.deleteFromTheList (myList, name); System. out. println ("Are there Employees leaving? "); String = in. nextLine () ;}for (Employee_1 employee_1: newList) {System. out. println ("Number of employees:" + newList. size () ;}} private List <Employee_1> deleteFromTheList (List <Employee_1> myList, String name) {int index = 0; boolean hasTheElement = false; for (int I = 0; I <myList. size (); I ++) {if (myList. get (I ). getEmployeeName (). equals (name) {index = I; hasTheElement = true ;}} if (! HasTheElement) {System. out. println ("This element does not exist") ;}else {myList. remove (index) ;}return myList ;}}

When Will ArrayList and rule list be used?

ArrayList, the validity rate of traversal elements and random access elements is relatively high, and the efficiency is relatively low when inserting and deleting elements.

Into list, the insertion and deletion efficiency will be relatively high, but the query efficiency will be relatively low

 

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.