21.1Elicit Problems
A question?
Before we learned the array, fully realized the superiority of the array, that is can store the same type of data, but we assume that there is such a demand, we see how to solve?
Please do a company staff salary management system, the following functions are required:
1. When a new employee is available, the employee is added to the management system;
2, according to the employee number, display the information of the employee;
3, can display all employee information;
4, can modify the salary of employees;
5, when the employee leaves, the staff from the management system to remove;
6, can be ranked according to salary from low to high order;
7. The average salary, minimum and maximum wage of the employee can be counted.
Solution - linked list or collection class:
We think that if there is an array that can be changed dynamically, theJava designers provide us with a series of collection classes.
21.2Collection Class Classification
In the figure, the red triangle is used for several collection classes that need to be mastered.
As you can see from the diagram above, there are several main Java collection classes:
1,List Structure of the collection class
ArrayList class,linkedlist(linked list) class,Vector class,Stack class
2,theMap Structure of the collection class
HashMap class,Hashtable class
3,set structure of the set class
HashSet class,TreeSet class
4 . Collection of Queue structures
Queue Interface
21.3 Listcollection classes of Structures
ArrayList class,linkedlist(linked list) class,Vector class,Stack class
21.3.1 ArrayList
The in-memory run that was drawn for the combined code.
The demo code is as follows:
1 /**Date: 2016-03-072 * Function: ArrayList demo3 */4 Packagetest;5 //all collection classes are in the Java.util package6 ImportJava.util.*;7 Public classDemo2 {8 9 Public Static voidMain (string[] args) {Ten //TODO auto-generated Method Stub One AArrayList al=NewArrayList (); -System.out.println ("At this time the length of Al is" +al.size ()); - theClerk clerk1=NewClerk ("Song Jiang", 50,1000); -Clerk Clerk2=NewClerk ("Wu use", 45,1200); -Clerk clerk3=NewClerk ("Lin", 35,1500); - + //Add an Employee object - //There are two add methods, and the following is the way to add a new object to the last ArrayList + //There is also an Add method that adds a new object to the specified location A Al.add (CLERK1); at Al.add (CLERK2); - Al.add (CLERK3); -Al.add (CLERK1);//Note: You can add an object repeatedly -System.out.println ("At this time the length of Al is" +al.size ()); - - //Traverse al, print out the name of each employee in for(intI=0;i<al.size (); i++) - { to //a temporary variable needs to be instantiated to complete the read of the name +Clerk temp=(Clerk) Al.get (i); -System.out.println ("+i+" the name of the individual is: "+temp.getname ()); the } * $System.out.println ("= = = = = = = = = = =");Panax Notoginseng //Delete an Employee object -Al.remove (1); the for(intI=0;i<al.size (); i++) + { AClerk temp=(Clerk) Al.get (i); theSystem.out.println ("+i+" the name of the individual is: "+temp.getname ()); + } - } $ $ } - - classClerk the { - PrivateString name;Wuyi Private intAge ; the Private intsalary; - Wu PublicString GetName () { - returnname; About } $ Public voidsetName (String name) { - This. Name =name; - } - Public intGetage () { A returnAge ; + } the Public voidSetage (intAge ) { - This. Age =Age ; $ } the Public intgetsalary () { the returnsalary; the } the Public voidSetsalary (intsalary) { - This. Salary =salary; in } the the PublicClerk (String name,intAgeintsalary) About { the This. name=name; the This. age=Age ; the This. salary=salary; + } - the}
View Code
The results of the operation are as follows:
1 at this point, the length of Al is 0 2 at this point, the length of Al is 4 3 the No. 0 person's name is: Song Jiang 4 the 1th person's name is: Wu uses 5 the 2nd person's name is: Lin 6 the 3rd person's name is: Song Jiang 7 = = Delete after Wu use = = 8 the No. 0 person's name is: Song Jiang 9 1th person's name is: Lin The 2nd person's name is: Song Jiang
View Code
21.2.2 LinkedList
Linked list
More methods than ArrayList , such as AddFirst(to the front Plus),addlast(add to the back) and so on.
21.2.3 Vector
Vector
There is a difference with ArrayList, and then speaking.
21.2.4 Stack
Stack
Add is added to the front.
Hanshunping Progressive Java 21st Lecture set (not to be continued)