找回密码
 立即注册
theIteratorthisallStudents | 企业管理 2022-09-23 125 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
JAVA按以下首要次序
·面向目标
·类集结构
·JDBC
·文件编程
本季方案
首要解说以下的内容:
·Collection
·List
·Set
·Map
·Iterator
·ListIteator
·Enumeration
·for…each句子(JDK1.5的新特性)
·Comparable接口
·二叉树
1、类集结构
(1)类集的产生意图
类集简单来讲即是一个动态的目标数组,此目标数组可以改动大小,可以恣意的终结目标的添加、删去、输出。一切的类集寄存java.util包中。
(2)类集的区别
1、寄存单值:Collection
2、寄存一对值:Map
3、输出:Iterator
(3)类集的详细概念
lt;1gt;Collection(只能寄存一个值)
-首要功能:用于输出使用
-子接口<imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>ist(许可有重复的元素,并且参加的次序即是输出的次序)
-子类:ArrayList,是在Java2以后推出的,是新的类,是使用异步管理方式,其功能较高
-子类:Vector,是在JDK1.0的时候就推出,由于是旧的类,有很多List所没有的功能,是使用同步管理方式,其线程安全性较高,会比较慢。使用Vector除了可以使用Iterator输出之外,也可以使用Enumeration进行输出。两者是通用的。Vector子类许可直接使用addElement(Objectobj),也是可以向调集中参加元素的。
-自己新增的方法:
-取每一个目标:publicObjectget(intind);
-子类:Stack(栈)
-子接口:Set(不许可有重复元素,重复元素会更换)?如何可以区别重复元素呢?
-子类:HashSet:是无序列寄存
-子类:TreeSet:有序寄存,是通过Comparable接口终结的
-常用方法:
-添加一个目标:publicbooleanadd(Objectobj)
-取得类集的长度:publicintsize();
-判别调集中的内容是不是为空:publicbooleanisEmpty()
-删去一个目标:publicbooleanremove(Objectobj):就必须牵扯到目标的比较状况。
-实例化Iterator目标:publicIteratoriterator();
lt;2gt;JDK1.5提供了一个方便的输出操作:for…each句子。
lt;3gt;Iterator接口输出是最常见的
-常用方法
-publicbooleanhasNext():判别是不是有下一个元素
-publicObjectnext():取出下一个元素
-子接口<imgsrc="sweat.gif"smilieid="10"border="0"alt=""/>istIterator,可以进行双向输出,只关于List接口有效
-常用方法:具有了Iterator接口中的全面内容
-publicbooleanhasPrevious()
-publicObjectprevious()
lt;4gt;Enumeration(在1.5以后也参加、泛型支持哈~)
-常用方法:
-判别是不是有下一个元素:publicbooleanhasMoreElements()
-取出元素:publicObjectelement()
5、Map(寄存一对值)
-功能:与Collection的功能不一样,Map的首要功能是用于搜索使用的。
-注意点:
-使用Map操作时,不能直接使用Iterator进行输出。
-并且里面不能有重复key。
-假如非要使用Iterator进行输出,则必须按以下的过程进行操作:
-Map--gt;Set--gt;Iterator--gt;Map.Entry--gt;key与value的分离
-常用子类:
-HashMap:无序寄存,是新的类,是JDK1.2时推出的,是异步管理,功能较高
-Hashtable:是旧的类,是JDK1.0时推出的,是线程安全的,功能较低
-Properties(要点):属性文件操作类
-TreeMap:有序寄存
-常用方法:
-向调集中保留数据:publicObjectput(Objectkey,Objectvalue):回来的是value
-从调集中搜索数据:publicObjectget(Objectkey):依据key搜索。
-将Map数据变为Set实例:Setlt;Map.Entrylt;K,Vgt;gt;entrySet()
-将全面的key变为set实例:Setlt;Kgt;keySet()
-将全面的vlaue变为Collection接口实例:Collectionlt;Vgt;values()
-Map.Entry:保留key与value
-publicObjectgetKey()
-publicObjectgetValue()
-寄存key的注意点:
################Michael分割线####################
2、代码解说
(6)实例一
例如,一个校园里有多个学生?问如何可以发挥出联系
一对多哈,类似数据库
importjava.util.
;
clasS/SChool{
privateStringname;
//一个校园有多个学生
privateListlt;Studentgt;allStudents;
publicSchool(Stringname){
this.setName(name);
this.setAllStudents(newArrayListlt;Studentgt;());
}
//向校园中参加学生
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;
//一个学生归于一个校园
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(张三,20);
Studentstu2=newStudent(李四,25);
Studentstu3=newStudent(王五,23);
Studentstu4=newStudent(赵六,28);
Studentstu5=newStudent(孙七,35);
//设置联系
sch.add(stu1);
stu1.setSchool(sch);
sch.add(stu2);
stu2.setSchool(sch);
sch.add(stu3);
stu3.setSchool(sch);
sch.add(stu4);
stu4.setSchool(sch);
[零基础学JAVA]JavaSE使用部分-35.JAVA类集之二.doc

(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)

[下载]11230774235.rar




上一篇:[零基础学JAVA]Java SE应用部分-35.JAVA类集之一
下一篇:[零基础学JAVA]Java SE应用部分-35.JAVA类集之三