(JAVA in the following order of priority
· Goal-oriented
·Class set structure
·JDBC
·File programming
Program this season
First explain the following:
·Collection
·List
·Set
·Map
·Iterator
·ListIterator
·Enumeration
·for...each sentence (new feature of JDK1.5)
Comparable interface
·Binary tree
1. Class set structure
(1) The intention of generating the class set
The class set is simply a dynamic target array, the target array can be changed in size, and the addition, deletion and output of the target can be arbitrarily terminated. All classes are stored in the java.util package.
(2) Differences in class sets
1. Consignment single value: Collection
2. Register a pair of values: Map
3. Output: Iterator
(3) Detailed concept of class set
lt;1gt;Collection (only one value can be registered)
- Primary function: for output use
- Sub-interface <imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>ist (repeated elements are allowed, and the order of participation is the order of output)
-Subclass: ArrayList, which was launched after Java2, is a new class, uses asynchronous management, and has higher functions
-Subclass: Vector, which was launched in JDK1.0. Because it is an old class, it has many functions that List does not have. It uses the synchronization management method, and its thread safety is high and it will be slower. In addition to using the Iterator output, you can also use the Enumeration for output using Vector. Both are generic. The Vector subclass allows direct use of addElement(Objectobj), and can also add elements to the collection.
-Added methods by yourself:
- Take each object: publicObjectget(intind);
- Subclass: Stack (stack)
- Sub-interface: Set (repeated elements are not allowed, repeated elements will be replaced)? How can we distinguish repeated elements?
- Subclass: HashSet: is an unordered register
- Subclass: TreeSet: Ordered storage, which is terminated through the Comparable interface
-Common method:
- add an object: publicbooleanadd(Objectobj)
- Get the length of the class set: publicintsize();
- Determine whether the content in the set is empty: publicbooleanisEmpty()
-Deleting an object: publicbooleanremove(Objectobj): must involve the comparison of the object.
- Instantiate the Iterator target: publicIteratoriterator();
lt;2gt;JDK1.5 provides a convenient output operation: for...each sentence.
lt;3gt;Iterator interface output is the most common
-Common methods
-publicbooleanhasNext(): Determine if there is a next element
-publicObjectnext(): take out the next element
- The sub-interface <imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>istIterator, which can perform bidirectional output, is only valid for the List interface
-Common methods: with comprehensive content in the Iterator interface
-publicbooleanhasPrevious()
-publicObjectprevious()
lt;4gt;Enumeration (also participate after 1.5, generic support ha~)
-Common method:
- Determine if there is a next element: publicbooleanhasMoreElements()
- Take out elements: publicObjectelement()
5. Map (register a pair of values)
-Function: Unlike the function of Collection, the primary function of Map is for searching.
-be careful:
- When using Map operations, iterator cannot be used directly for output.
-And there cannot be duplicate keys in it.
-If you have to use Iterator for output, you must do the following process:
-Map--gt;Set--gt;Iterator--gt;Map.Entry--gt;Separation of key and value
-Common subclasses:
-HashMap: unordered storage, is a new class, launched in JDK1.2, asynchronous management, high function
-Hashtable: It is an old class, launched when JDK1.0, it is thread-safe and has lower functions
-Properties (points): Properties file operation class
-TreeMap: ordered storage
-Common method:
-Retain data in the call set: publicObjectput(Objectkey, Objectvalue): The value returned is
-Search data from the collection: publicObjectget(Objectkey): Search by key.
- Turn Map data into Set instances: Setlt;Map.Entrylt;K,Vgt;gt;entrySet()
- Turn a comprehensive key into a set instance: Setlt;Kgt;keySet()
- Turn a comprehensive vlaue into an instance of the Collection interface: Collectionlt;Vgt;values()
-Map.Entry: keep key and value
-publicObjectgetKey()
-publicObjectgetValue()
-Notes on storing keys:
################Michael dividing line####################
2. Code Explanation
(6) Example 1
For example, multiple students on a campus? Ask how you can make connections
One-to-Doha, similar database
importjava.util.
;
classS/SChool{
privateStringname;
// Multiple students on a campus
privateListlt;Studentgt;allStudents;
publicSchool(Stringname){
this.setName(name);
this.setAllStudents(newArrayListlt;Studentgt;());
}
//To participating students on campus
publicvoidadd(Studentstu){
this.allStudents.add(stu);
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetName(){
returnthis.name;
}
publicvoidsetAllStudents(Listlt;Studentgt;allStudents){
this.allStudents=allStudents;
}
publicListlt;Studentgt;getAllStudents(){
returnthis.allStudents;
}
}
classStudent{
privateStringname;
privateintage;
//A student is assigned to a campus
privateSchoolschool;
publicStudent(Stringname,intage){
this.setName(name);
this.setAge(age);
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetName(){
returnthis.name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicintgetAge(){
returnthis.age;
}
publicvoidsetSchool(Schoolschool){
this.school=school;
}
publicSchoolgetSchool(){
returnthis.school;
}
}
publicclassDemo11{
publicstaticvoidmain(Stringargs){
Schoolsch=newSchool(51cto);
Studentstu1=newStudent(Zhangsan,20);
Studentstu2=newStudent(Li Si,25);
Studentstu3=newStudent(Wang Wu, 23);
Studentstu4=newStudent(Zhao Liu,28);
Studentstu5=newStudent(Sun Seven,35);
//set contact
sch.add(stu1);
stu1.setSchool(sch);
sch.add(stu2);
stu2.setSchool(sch);
sch.add(stu3);
stu3.setSchool(sch);
sch.add(stu4);
stu4.setSchool(sch);
[Zero-based JAVA] JavaSE use part-35.JAVA class set 2.doc)