找回密码
 立即注册
theIterator使用排序 | 企业管理 2022-09-23 96 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、代码解说
(8)Vector
使用List接口接收Vector的目标
与Demo10类似,就换了一个子类Vector哈,看下作用:
使用Enumeration进行输出
Vector子类许可直接使用addElement(Objectobj),也是可以向调集中参加元素的,看下作用:
(9)TreeSet
看下作用,进行排序了哈~
如何排序的?是恣意的目标都可以排序吗?验证一下哈~!
看下作用,出错了哈~
假设自己写了一个目标,则无法排序了
TreeSet排序是如何进行管理的?使用了比较器(Comparable接口)
假设一个类想使用TreeSet进行排序操作,则目标所在的类必定要实现Comparable接口。一起指定排序的规则(二叉树)。
把以上的内容实现一下:之前的String能否排序?必定可以。
((Student)obj).name向下转型哈~
看下作用:
把以上的需要改一下:之前我留过一道标题:
声明一个学生类,里边有名字、年纪、成果属性,按成果由高到低排序,假设成果持平,则按年纪进行由低到高排序。
lt;1gt;、可以直接使用TreeSet进行排序
lt;2gt;、可以直接使用Arrays.sort方法排序
此两种方法的核心实现没有任何区别。
Comparable接口中的compareTo()方法只能回来三个值
·假设小于:则回来-1
·假设大于:则回来1
·假设等于:则回来0
看下作用:
假设成果一样,则按年纪从低到高排序哈~
看下作用:
假设此处使用了TreeSet,则向里边添加内容也正常了
看下作用:
上面排序的原理是二叉算法哈~
(10)二叉算法(理解)
假设如今有以下的字符串:
“A”、“X”、“B”,“Y”、“Z”,如何进行排序呢?
A
X
B
Y
Z
中序遍历:ABXYZ---gt;排序
Demo21:
importjava.util.
;
classBi

(JAVA in the following important order
· 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 purpose of generating the class set
The class set is simply a dynamic target array, the target array can be changed in size, and the target can be added, deleted, and output at will. All classes are stored in the java.util package.
(2) Division of clusters
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 methods, 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 a 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. 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, implemented 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.
- Assuming that you have to use Iterator for output, you must do the following process:
-Map--gt;Set--gt;Iterator--gt;Map.Entry--gt;The difference between 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 (emphasis): 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
(8)Vector
Use the List interface to receive the target of the Vector
Similar to Demo10, a subclass of Vector has been changed. Let's see the effect:
Use Enumeration for output
The Vector subclass allows to use addElement(Objectobj) directly, and can also add elements to the call set, see the effect below:
(9)TreeSet
Take a look at the effect and sort it~
How to sort? Can any target be sorted? Verify it~!
Look at the effect, it's wrong~
Suppose you write a goal yourself, you can't sort it
How is TreeSet sorting managed? Comparable (Comparable interface) is used
Suppose a class wants to use TreeSet for sorting operation, the target class must implement the Comparable interface. Together specify the ordering rules (binary trees).
Implement the above content: Can the previous String be sorted? It must be.
((Student)obj).name downcast ha~
Take a look at the effect:
Change the above needs: I left a title before:
Declare a student class, which contains name, age, and achievement attributes, sorted by achievement from high to low, assuming that the results are the same, then sort by age from low to high.
lt;1gt;, you can directly use TreeSet to sort
lt;2gt;, you can use the Arrays.sort method to sort directly
There is no difference in the core implementation of the two methods.
The compareTo() method in the Comparable interface can only return three values
·If less than: return -1
·If greater than: return 1
·If equal to: then return 0
Take a look at the effect:
Assuming the results are the same, then sort by age from low to high~
Take a look at the effect:
Assuming that TreeSet is used here, it is normal to add content to it
Take a look at the effect:
The principle of the above sorting is a binary algorithm~
(10) Binary algorithm (understanding)
Suppose we now have the following strings:
How to sort "A", "X", "B", "Y", "Z"?
A
X
B
Y
Z
In-order traversal: ABXYZ---gt; sort
Demo21:
importjava.util.
;
classBi)

[下载]11232343398.rar




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