当前位置: 首页 > news >正文

建设风景区网站的目的红酒网站设计

建设风景区网站的目的,红酒网站设计,seo技术外包公司,wordpress 运费模板下载简介集合和数组的区别#xff1a;数组存储基础数据类型#xff0c;且每一个数组都只能存储一种数据类型的数据#xff0c;空间不可变。集合存储对象#xff0c;一个集合中可以存储多种类型的对象。空间可变。严格地说#xff0c;集合是存储对象的引用#xff0c;每个对象…简介集合和数组的区别数组存储基础数据类型且每一个数组都只能存储一种数据类型的数据空间不可变。集合存储对象一个集合中可以存储多种类型的对象。空间可变。严格地说集合是存储对象的引用每个对象都称为集合的元素。根据存储时数据结构的不同分为几类集合。但对象不管存储到什么类型的集合中既然集合能存储任何类型的对象这些对象在存储时都必须向上转型为Object类型也就是说集合中的元素都是Object类型的对象。既然是集合无论分为几类它都有集合的共性也就是说虽然存储时数据结构不一样但该有的集合方法还是得有。在java中Collection接口是集合框架的根接口所有集合的类型都实现了此接口或从其子接口中继承。Collection接口根据数据结构的不同一些collection允许有重复的元素而另一些则不允许。一些collection是有序的而另一些则是无序的。Java SDK不提供直接继承自Collection的类Java SDK提供的类都是继承自Collection的子接口如List和Set。也就是说无法直接new一个collection对象而是只能new一个实现Collection类的子接口的对象如new ArrayList();。所有的Collection类都必须至少提供两个构造方法无参数构造方法构造一个空集合带Collection参数的构造方法构造一个包含该Collection内容的集合。例如ArrayList就有3个构造方法其中之二就满足这两个构造方法的要求。Collection是java.util包中的类因此要实现集合的概念需要先导入该包。ArrayList继承自List接口List接口又继承自Collection接口。ArrayList类存储的集合中元素有序、可重复。import java.util.*;Collection coll new ArrayList();因为Collection接口不允许直接实现因此需要通过实现它的子类来实现集合的概念此处创建的是ArrayList对象使用了父类引用好处是扩展性较好。Collection有一些集合的通用性操作方法分为两类一类是普通方法一类是带有All的方法这类方法操作的是集合。add()向集合的尾部插入元素返回值类型为boolean插入成功返回true。注意集合只能存储对象(实际上是对象的引用)。import java.util.*;//public class TestColl {public static void main(String[] args) {Collection coll new ArrayList();coll.add(abcd);//插入字符串对象coll.add(123);//插入Int对象coll.add(123);coll.add(new Student(Gaoxiaof,23));//插入Student对象coll.add(new Student(Gaoxiaof,23));//插入另一个Student对象System.out.println(coll);//直接输出集合中的元素得到结果[abcd,123,123,Gaoxiaof 23,Gaoxiaof 23]}}//class Student {private String name;private int age;Student(String name,int n) {this.name name;this.age n;}public String getName() {return this.name;}public int getAge() {return this.age;}public String toString() {return this.name this.age;}}上面插入的abcd和123都是经过自动装箱转换为对象后存储在集合中的。其中两个add(123)是重复的对象元素因为判断集合中的元素是否重复的唯一方法是equals方法是否返回0。Integer已经重写过equals()。而后面的两个Student对象是不同对象因为Student类中没有重写equals()方法所以它们是不重复的元素。remove()删除集合中首次出现的元素。确定是否能删除某个元素唯一的方法是通过equals()方法确定对象是否相等相等时删除才返回true。Collection coll new ArrayList();coll.add(abcd);coll.add(new Integer(128));coll.add(new Student(Gaoxiaofang,23));System.out.println(coll.remove(new Integer(128))); //truecoll.remove(new Student(Gaoxiaofang,23)); //false因为没有重写equals()System.out.println(coll); //return: [abcd,Gaoxiaofang 23]clear()清空该集合中的所有元素。contains(object obj)是否包含某对象元素。判断的依据仍然是equals()方法。Collection coll new ArrayList();coll.add(new Integer(128));System.out.println(coll.contains(new Integer(128))); //trueisEmpty()集合是否不包含任何元素。size()返回该集合中元素的个数。equals(Object obj)比较两个集合是否完全相等。依据是集合中的所有元素都能通过各自的equals得到相等的比较。addAll(Collection c)将整个集合c中的元素都添加到该集合中。containsAll(Collection c)该集合是否包含了c集合中的所有元素即集合c是否是该集合的子集。removeAll(Collection c)删除该集合中那些也包含在c集合中的元素。即删除该集合和c集合的交集元素。retainAll(Collection c)和removeAll()相反仅保留该集合中和集合c交集部分的元素。iterator(Collection c)返回此集合中的迭代器注意返回值类型为Iterator。迭代器用于遍历集合。见下文。Iterator通用迭代器因为不同类型的集合存储数据时数据结构不同想要写一个通用的遍历集合的方法是不现实的。但无论是哪种类型的集合只有集合自身对集合中的元素是最了解的因此在实现Collection接口时不同集合类都实现了自己独有的遍历方法这称为集合的迭代器Iterator。其实Collection继承了java.lang.Iterable接口该接口只提供了一个方法iterator()只要是实现了这个接口的类就表示具有迭代的能力也就具有foreach增强遍历的能力。迭代器自身是一个接口通过Collection对象的iterator()方法就可以获取到对应集合类型的迭代器。例如Collection coll new ArrayList();Iterator it coll.iterator(); //获取对应类型的集合的迭代器Iterator接口提供了3个方法hasNext()判断是否有下一个元素。Next()获取下一个元素。注意它返回的是Object(暂不考虑泛型)类型。remove()移除迭代器最后返回的一个元素。此方法为Collection迭代过程中修改元素的唯一安全的方法。虽然有不同种类型的集合但迭代器的迭代方法是通用的。例如要遍历coll集合中的元素。import java.util.*;public class TestColl {public static void main(String[] args) {Collection coll new ArrayList();coll.add(abcd);coll.add(new Integer(129));coll.add(new Student(Gaoxiaofang,23));Iterator it coll.iterator();while (it.hasNext()) { //Iterator遍历的方法System.out.println(it.next()); //return:abcd,129,Gaoxiaofang 23}}}class Student {private String name;private int age;Student(String name,int n) {this.name name;this.age n;}public String getName() {return this.name;}public int getAge() {return this.age;}public String toString() {return this.name this.age;}}但是通常来说上面的遍历方式虽然正确但下面的遍历方式更佳。因为it对象只用于集合遍历遍历结束后就应该消失所以将其放入到for循环的内部由于for循环的第三个表达式缺失所以不断地循环第二个表达式即可。for (Iterator it coll.iterator();it.hasNext();) {System.out.println(it.next());}通过Iterator遍历到的元素是集合中的一个对象对象也是有属性的。如何引用这些属性只需将遍历出的元素作为对象来使用即可但由于next()返回的元素都是Object对象直接操作这个元素对象无法获取对应元素中的特有属性。因此必须先强制对象类型转换。例如获取coll中为Student对象元素的name属性并删除非Student对象的元素。Collection coll new ArrayList();coll.add(abcd);coll.add(new Integer(129));coll.add(new Student(Gaoxiaofang,23));for (Iterator it coll.iterator();it.hasNext();) {Object obj it.next();if (obj instanceof Student) {Student s (Student)obj;System.out.println(s.getName()); //return: Gaoxiaofang} else {it.remove();}}System.out.println(coll); //return: [Gaoxiaofang 23]因为集合中有些非Student对象元素因此需要判断it.next()是否满足instanceof的要求但不能直接写为下面的代码for (Iterator it coll.iterator();it.hasNext();) {if (it.next() instanceof Student) {Student s (Student)it.next();System.out.println(s.getName());}}因为每执行一次it.next()元素的游标指针就向下滑动1在这个写法中if判断表达式中使用了一次it.next()在if的代码块中又调用了一次it.next()。所以应该将it.next()保存到对象变量中。而it.next()返回的类型是Object类型因此定义Object obj it.next()。只有remove()方法是Iterator迭代器迭代过程中修改集合元素且安全的方法。以迭代时add()为例当开始迭代时迭代器线程获取到集合中元素的个数当迭代过程中执行add()时它将采用另一个线程来执行(因为add()方法不是Iterator接口提供的方法)结果是元素个数就增加了且导致新增的元素无法确定是否应该作为迭代的一个元素。这是不安全的行为因此会抛出ConcurrentModificationException异常。而remove()方法是迭代器自身的方法它会使用迭代器线程来执行因此它是安全的。对于List类的集合来说可以使用Iterator的子接口ListIterator来实现安全的迭代该接口提供了不少增删改查List类集合的方法。List接口List接口实现了Collection接口。List接口的数据结构特性是1.有序列表且带索引index。所谓有序指先后插入的顺序即Index决定顺序。而向Set集合中插入数据会被打乱2.大小可变。3.数据可重复。4.因为有序和大小可变使得它除了有Collection的特性还有根据index精确增删改查某个元素的能力。5.实现List接口的两个常用类为(1).ArrayList数组结构的有序列表1).长度可变可变的原因是在减少或添加元素时部分下标整体减一或加一如果已分配数组空间不够则新创建一个更大的数组并拷贝原数组的内存(直接内存拷贝速度极快)2).查询速度快增删速度慢。查询快是因为内存空间连续增删速度慢是因为下标移动。3).除了ArrayList是不同步列表它几乎替代了Vector类。(2).LinkedList链表结构的有序列表1).不同步2).增删速度快查询速度慢。增删速度快的原因是只需修改下链表中前后两个元素的索引指向即可3).能够实现堆栈(后进先出LIFO,last in first out)、队列(queue,通常是FIFO,first in first out)和双端队列(double ends queue)。ArrayList类的方法和List方法基本一致所以下面介绍了List通用方法和ListIterator就没必要介绍ArrayList。但LinkedList比较特殊所以独立介绍。List接口通用方法除了因为继承了Collection而具有的通用方法外对于List接口也有它自己的通用方法。一般List的这些通用方法针对的是序列的概念。有了序列和下标索引值可以精确地操控某个位置的元素包括增删改查。(1).增add(index,element)(2).删remove(index)、remove(obj)删除列表中第一个obj元素(3).改set(index,element)(4).查get(index)(5).indexOf(obj)返回列表中第一次出现obj元素的索引值如不存在则返回-1(6).lastIndexOf(obj)(7).subList(start,end)返回列表中从start到end(不包括end边界)中间的元素组成列表。注意返回的是List类型。(8).listIterator()返回从头开始遍历的List类集合的迭代器ListIterator。(9).listIterator(index)返回从index位置开始遍历的List结合迭代器ListIterator。因为有了get()方法除了Iterator迭代方式还可以使用get()方法遍历集合List l new ArrayList();for (int i0;iSystem.out.println(l.get(i));}但注意这种方法不安全因为l.size()是即时改变的如果增删了元素size()也会随之改变。示例import java.util.*;public class TestList {public static void main(String[] args) {List ls new ArrayList();ls.add(new Student(Malong1,21));ls.add(new Student(Malong2,22));ls.add(1,new Student(Malong3,23));//[Malong1 21,Malong3 23,Malong2 22]System.out.println(ls.indexOf(new Student(Malong3,23)));// return:1ls.set(2,new Student(Gaoxiao1,22));//[Malong1 21,Malong3 23,Gaoxiao1 22]for (Iterator it l.iterator();it.hasNext();) {//第一种迭代Student stu (Student)it.next();if (stu.getAge() 22) {it.remove();// the safe way to operate element//ls.add(new Student(Malong4,24)); //throw ConcurrentModificationException}}//[Malong1 21,Malong3 23]System.out.println(l\n---------------);for (int i0;i//第二种迭代System.out.println(ls.get(i));}}}class Student {private String name;private int age;Student(String name,int n) {this.name name;this.age n;}public String getName() {return this.name;}public int getAge() {return this.age;}//override toString()public String toString() {return this.name this.age;}//override equals()public Boolean equals(Object obj) {if (this obj) {return true;}if (!(obj instanceof Student)) {throw new ClassCastException(Class error);}Student stu (Student)obj;return this.name.equals(stu.name) this.age stu.age;}}上面的代码中如果将ls.add(new Student(Malong4,24));的注释取消将抛出异常因为Iterator迭代器中唯一安全操作元素的方法是Iterator接口提供的remove()而add()方法是List接口提供的而非Iterator接口的方法。但对于List集合类来说可以使用ListIterator迭代器它提供的操作元素的方法更多因为是迭代器提供的方法因此它们操作元素时都是安全的。List集合的迭代器ListIterator通过listIterator()方法可以获取ListIterator迭代器。该迭代器接口提供了如下几种方法hasNext()是否有下一个元素hasPrevious()是否有前一个元素用于逆向遍历next()获取下一个元素previour()获取前一个元素用于逆向遍历add(element)插入元素。注这是迭代器提供的add()而非List提供的add()remove()移除next()或previous()获取到的元素。注这是迭代器提供的remove()而非List提供的remove()set(element)设置next()或previour()获取到的元素。注这是迭代器提供的set()而非List提供的set()例如前文示例在Iterator迭代过程中使用List的add()添加元素抛出了异常此处改用ListIterator迭代并使用ListIterator提供的add()方法添加元素。List l new ArrayList();l.add(new Student(Malong1,21));l.add(new Student(Malong2,22));l.add(1,new Student(Malong3,23)); //[Malong1 21,Malong3 23,Malong2 22]l.set(2,new Student(Gaoxiao1,22));//[Malong1 21,Malong3 23,Gaoxiao1 22]for (ListIterator li l.listIterator();li.hasNext();) {Student stu (Student)li.next();if (stu.getAge() 22) {//l.add(new Student(Malong4,24)); //throw ConcurrentModificationExceptionli.add(new Student(Malong4,24));}}LinkedList集合LinkedList类的数据结构是链表类的集合。它可以实现堆栈、队列和双端队列的数据结构。其实实现这些数据结构都是通过LinkedList提供的方法按照不同逻辑实现的。提供的其中几个方法如下因为是实现了List接口所以除了下面的方法还有List接口的方法可用。addFirst(element)向链表的首部插入元素addLast(element)向链表的尾部插入元素getFirst()获取链表的第一个元素getLast()获取链表最后一个元素removeFirst()移除并返回第一个元素注意返回的是元素removeLast()移除并返回最后一个元素注意返回的是元素LinkedList模拟队列数据结构队列是先进先出FIFO的数据结构。封装的队列类MyQueue代码如下import java.util.*;class MyQueue {private LinkedList mylist;MyQueue() {mylist new LinkedList();}// add element to queuepublic void add(Object obj) {mylist.addFirst(obj);//Fisrt In}//get element from queuepublic Object get() {return mylist.removeLast();//First Out}//queue is null?public Boolean isNull() {return mylist.isEmpty();}//the size of queuepublic int size() {return mylist.size();}//remove element in queue by indexpublic Boolean remove(int index) {if(this.size()-1 index) {throw new IndexOutOfBoundsException(index too large!);}mylist.remove(index);return true;}//remove the first appearance element in queue by Objectpublic Boolean remove(Object obj) {return mylist.remove(obj);}public String toString() {return mylist.toString();}}操作该队列数据结构程序代码如下import java.util.*;public class FIFO {public static void main(String[] args) {MyQueue mq new MyQueue();mq.add(Malong1);mq.add(Malong2);mq.add(Malong3);mq.add(Malong4); //[Malong4,Malong3,Malong2,Malong1]System.out.println(mq.size()); //return:4mq.remove(2); //[Malong4,Malong3,Malong1]mq.remove(Malong1); //[Malong4,Malong3]System.out.println(mq);while (!mq.isNull()) {System.out.println(mq.get());}}}Set接口Set接口也实现了Collection接口。它既然能单独成类它和List集合的数据结构一定是大有不同的。Set接口的数据结构特性是1.Set集合中的元素无序。这里的无序是相对于List而言的List的有序表示有下标Index的顺序而Set无需是指没有index也就没有顺序。2.Set集合中的元素不可重复。3.因为无序因此Set集合中取出元素的方法只有一种迭代。4.实现Set接口的两个常见类为(1).HashSethash表数据结构1).不同步2).查询速度快3).判断元素是否重复的唯一方法是先调用hashcode()判断对象是否相同相同者再调用equals()方法判断内容是否相同。所以要将元素存储到此数据结构的集合中必须重写hashcode()和equals()。(2).TreeSet二叉树数据结构1).二叉树是用来排序的因此该集合中的元素是有序的。这个有序和List的有序概念不同此处的有序指的是存储时对元素进行排序例如按照字母顺序数字大小顺序等而非index索引顺序。2).既然要排序而equals()方法只能判断是否相等。因此数据存储到TreeSet集合中时需要能够判断大小。3).有两种方法用于构造有序的TreeSet集合a.待存储对象的类实现Comparable接口并重写它的compareTo()方法b.在构造TreeSet集合时指定一个比较器comparator。这个比较器需要实现Comparator接口并重写compare()方法。(3).LinkedHashSet链表形式的HashSet仅在HashSet上添加了链表索引。因此此类集合有序(Linked)、查询速度快(HashSet)。不过很少使用该集合类型。HashSet集合HashSet的用法没什么可解释的方法都继承自Set再继承自Collection。需要说明的是它的无序性、不可重复性、计算hash值时的方法以及判断重复性时的方法。import java.util.*;public class TestHashSet {public static void main(String[] args) {Set s new HashSet();s.add(abcd4);s.add(abcd1);s.add(abcd2);s.add(abcd3);s.add(abcd1); //重复for (Iterator it s.iterator();it.hasNext();) {Object obj it.next();System.out.println(obj);}}}得到的结果是无序且元素是不可重复的abcd2abcd3abcd4abcd1这里判断字符串对象是否重复的方法是先调用String的hashcode()进行判断如果相同再调用String的equals()方法。其中String的hashcode()方法在计算hash值时是根据每个字符计算的相同字符位置处的相同字符运算结果相同。所以上面几个字符串对象中前缀abcd子串部分的hash运算结果相同最后一个字符决定了这些字符串对象是否相同。插入时有两个abcd1所以总共调用了一次String的equals()方法。如果是存储自定义的对象如Student对象该对象定义方式如下class Student {String name;int age;Student(String name,int n) {this.name name;this.age n;}//override toString()public String toString() {return this.name this.age;}//override equals()public boolean equals(Object obj) {if (this obj) {return true;}if (!(obj instanceof Student)) {return false;}Student stu (Student)obj;return this.name.equals(stu.name) this.age age;}}即使重写了equals()插入属性相同的Student对象到HashSet中时也会认为不重复的。import java.util.*;public class TestHashSet {public static void main(String[] args) {Set s new HashSet();s.add(new Student(Malong1,21));s.add(new Student(Malong1,21));s.add(new Student(Malong1,21));for (Iterator it s.iterator();it.hasNext();) {Object obj it.next();System.out.println(obj);}}}结果Malong1 21Malong1 21Malong1 21这是因为HastSet集合的底层首先调用Student的hashcode()方法而Student没有重写该方法而是继承自Object所以每个对象的hashcode()都不相同而直接插入到集合中。因此需要重写Student的hashcode()方法。以下是一种重写方法public int hashCode() {return this.name.hashCode() age*31; //31可以是任意数但不能是1或0。}如果不加上age*31那么name部分的hash值有可能是相同的但这很可能不是同一Student对象所以应该加上age属性作为计算hash值的一部分元素。但不能直接加age因为这样会导致new Student(lisi3,23)和new Student(lisi2,24)的hashcode相同(323224)因此需要为age做一些修改例如乘一个非0和1的整数。在Student中重写hashCode()后再插入下面这些Student对象就能相对精确地判断是否为同一个Student元素。s.add(new Student(lisi1,21));s.add(new Student(lisi1,21)); //此处将调用equals()且最终判断为重复对象s.add(new Student(lisi2,24));s.add(new Student(lisi3,23)); //此处将调用equals()s.add(new Student(Gaoxiao1,23));s.add(new Student(Gaoxiao2,21));s.add(new Student(Gaoxiao3,22));结果lisi1 21Gaoxiao1 23Gaoxiao3 22lisi2 24lisi3 23Gaoxiao2 21LinkedHashSet集合链表顺序的HashSet集合相比HashSet只需多记录一个链表索引即可这就使得它保证了存储顺序和插入顺序相同。实现方式除了new对象时和HashSet不一样其他任何地方都是一样的。import java.util.*;public class TestHashSet {public static void main(String[] args) {Set s new LinkedHashSet();s.add(new Student(lisi1,21));s.add(new Student(lisi1,21));s.add(new Student(lisi2,24));s.add(new Student(lisi3,23));s.add(new Student(Gaoxiao1,23));s.add(new Student(Gaoxiao3,21));s.add(new Student(Gaoxiao2,22));for (Iterator it s.iterator();it.hasNext();) {Object obj it.next();System.out.println(obj);}}}结果lisi1 21lisi2 24lisi3 23Gaoxiao1 23Gaoxiao3 21Gaoxiao2 22TreeSet集合TreeSet集合以二叉树数据结构存储元素。二叉树保证了元素之间是排过序且相互唯一的因此实现TreeSet集合最核心的地方在于对象之间的比较。比较对象有两种方式一是在对象类中实现Comparable接口重写compareTo()方法二是定义一个专门用于对象比较的比较器实现这个比较器的方法是实现Comparator接口并重写compare()方法。其中Comparable接口提供的比较方法称为自然顺序例如字母按照字典顺序数值按照数值大小顺序。无论是哪种方式每个待插入的元素都需要先转型为Comparable确定了将要存储在二叉树上的节点位置后然后再转型为Object存储到集合中。插入String类对象。由于String已经重写了compareTo()因此下面插入String对象到TreeSet集合中没有任何问题。import java.util.*;//public class TestTreeSet {public static void main(String[] args) {Set t new TreeSet();t.add(abcd2);t.add(abcd11);t.add(abcd3);t.add(abcd1);//t.add(23);//t.add(21);//t.add(21);for (Iterator it t.iterator();it.hasNext();) {Object obj it.next();System.out.println(obj);}}}但不能将上面t.add(23)等取消注释虽然Integer类也重写了compareTo()但在插入这些Integer类元素时集合中已经存在String类的元素String类的compareTo()和Integer的compareTo()的比较方法不一样使得这两类元素之间无法比较大小也就无法决定数值类的元素插入到二叉树的哪个节点。插入实现了Comparable接口且重写了compareTo()的自定义对象。例如Student对象如果没有重写compareTo()方法将抛出异常提示无法转型为Comparable。t.add(new Student(Malongshuai1,23));结果Exception in thread main java.lang.ClassCastException: Student cannot be cast to java.lang.Comparableat java.util.TreeMap.compare(Unknown Source)at java.util.TreeMap.put(Unknown Source)at java.util.TreeSet.add(Unknown Source)at TestTreeSet.main(TestTreeSet.java:8)所以修改Student重写compareTo()在重写应该考虑哪个作为主排序属性哪个作为次要排序属性。例如以name为主排序属性age为次排序属性。compareTo()返回正数则表示大于返回负数则表示小于返回0则表示等于。如下class Student implements Comparable {String name;int age;Student(String name,int n) {this.name name;this.age n;}public String toString() {return this.name this.age;}public int compareTo(Object obj) {if (!(obj instanceof Student)) {throw new ClassCastException(Class cast wrong!);}Student stu (Student)obj;// compare name first, then ageint temp this.name.compareTo(stu.name);return temp 0 ? this.age - stu.age :temp;}}于是插入Student时将根据name中的字母顺序相同时再根据age大小顺序最后如果都相同则认为元素重复不应该插入到集合中。t.add(new Student(Malongshuai1,23));t.add(new Student(Malongshuai3,21));t.add(new Student(Malongshuai2,23));t.add(new Student(Malongshuai1,23)); //重复t.add(new Student(Malongshuai1,22));结果Malongshuai1 22Malongshuai1 23Malongshuai2 23Malongshuai3 21使用比较器comparator实现排序。此时TreeSet的构造方法为TreeSet(Comparator comp)。当使用了比较器后插入数据时将默认使用比较器比较元素。比较器是一个实现了java.util.Comparator接口并重写了compare()方法的类可以根据不同比较需求创建不同的比较器。 例如创建一个根据age作为主排序属性name作为次排序属性的比较器SortByAge由于这个比较器是用来比较Student对象大小的因此必须先转型为Student。import java.util.*;//public class SortByAge implements Comparator {public int compare(Object o1,Object o2) {//Cast to Student firstif (!(o1 instanceof Student) || !(o2 instanceof Student)) {throw new ClassCastException(Wrong);}Student s1 (Student)o1;Student s2 (Student)o2;//compare age first, then nameint temp s1.age - s2.age;return temp 0 ? s1.name.compareTo(s2.name) : temp;}}指定TreeSet的比较器为SortByAge并插入一些Student对象public class TestTreeSet {public static void main(String[] args) {Set t new TreeSet(new SortByAge());t.add(new Student(Malongshuai1,23));t.add(new Student(Malongshuai3,21));t.add(new Student(Malongshuai2,23));t.add(new Student(Malongshuai1,23)); //重复t.add(new Student(Malongshuai1,22));for (Iterator it t.iterator();it.hasNext();) {Object obj it.next();System.out.println(obj);}}}当为TreeSet集合指定了比较器时结果将先按照age顺序再按照name排序的尽管Student类中仍然重写了compareTo()方法Malongshuai3 21Malongshuai1 22Malongshuai1 23Malongshuai2 23总结以上就是本文关于集合框架(Collections Framework)详解及代码示例的全部内容希望对大家有所帮助。感兴趣的朋友可以继续参阅本站如有不足之处欢迎留言指出。感谢朋友们对本站的支持
http://www.yutouwan.com/news/310816/

相关文章:

  • 营销型网站的建设与推广辅导记录建仿网站
  • 找人给公司做网站去哪找wordpress门户网站
  • 开一个网站建设公司好推广网站文案
  • 网站产品图片尺寸湖南网站seo营销
  • 如何搭建一个公司网站免费的企业网站建设
  • 福建省建设执业资格中心网站深圳网站建设企
  • 手机app与电脑网站的区别个人建设图片分享网站
  • 重生做明星那个网站下载营销型企业网站案例
  • 网站建设电商胖子马wordpress模板 q8免费版
  • 公司做网站好吗随州网站建设学校
  • 做茶歇的网站徐州h5模板建站
  • 用ps做网站设计微信网站链接怎么做
  • 有产品做推广 选哪个 网站开发小程序定制公司
  • 不备案的网站有那些网站开发网上宠物店管理系统
  • 网站美化教程下载做培训网站前端
  • 时光轴网站模板空间登录
  • 做网站不给源码上海互联网公司
  • 哪个网站注册域名好高端网站设计元素图片
  • 国外做锅炉的网站网站访问代理在线
  • 怎样建立个人网站?大学生网络营销策划书模板
  • 网站跳出率是什么意思ios软件开发需要学什么
  • 做旅行网站的意义物业网站建设
  • 怎样做交互式网站深圳网站制作公司排名
  • 官网网站备案网络游戏排行榜2021前十名手游
  • 广州建网站技术2345浏览器手机版
  • 个人网站 flash鲜花类网站建设策划书范文
  • 用宝塔做网站步骤点点 网站建设
  • 网站建设推广市场wordpress 七牛不更新
  • 网页设计与网站建设电话python做网站优势
  • 做智能网站平台已购买域名 如何做网站