Java容器类学习教案_第1页
Java容器类学习教案_第2页
Java容器类学习教案_第3页
Java容器类学习教案_第4页
Java容器类学习教案_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、会计学1Java容器类容器类第一页,编辑于星期六:十六点 四十九分。第1页/共35页第二页,编辑于星期六:十六点 四十九分。第2页/共35页第三页,编辑于星期六:十六点 四十九分。CollectionHashSet(Set)LinkedListVector, ArrayList(List)HashtableHashmap(Map)TreeSet(SortedSet)TreeMap(SortedMap)第3页/共35页第四页,编辑于星期六:十六点 四十九分。第4页/共35页第五页,编辑于星期六:十六点 四十九分。Collection 接口中所定义的方法:boolean add(Object el

2、ement);boolean remove(Object element);boolean contains(Object element);int size(); boolean isEmpty(); void clear();Iterator iterator();boolean containsAll(Collection c);boolean addAll(Collection c);boolean removeAll(Collection c);boolean retainAll(Collection c);Object toArray(); 第5页/共35页第六页,编辑于星期六:十

3、六点 四十九分。import java.util.*;public class CollectionTest public static void main (String args) Collection c = new ArrayList();/ 可以放入不同类型的对象c.add (hello);c.add (new Boolean(true);c.add (new Integer(100);System.out.println (size + c.size() + : + c);System.out.println (contains: + c.contains(new Integer(

4、100);System.out.println (c.remove(new Boolean(true);System.out.println (isEmpty: + c.isEmpty();System.out.println (size + c.size() + : + c);输出结果:size3: hello, true, 100contains: truetrueisEmpty: falsesize2: hello, 100第6页/共35页第七页,编辑于星期六:十六点 四十九分。public class Person private int id; private String name

5、;public Person (int id, String name) this. id = id; this. name = name;public int getId() return id; public String getName() return name; public void setId (int id) this. id = id;public void setName (String name) this. name = name;public String toString() return id: + id + |name: + name;第7页/共35页第八页,编

6、辑于星期六:十六点 四十九分。import java.util.*;public class CollectionTest1 public static void main (String args) Collection c = new HashSet();c. add (new Person(1, “c+);c. add (new Person(2, “java);System. out. println (c. size() + : + c);System. out. println (contains: + c. contains (new Person(2, java);System

7、.out. println (c. remove (new Person(2, java);System. out. println (c. size() + : + c);输出结果:2: id: 2|name: java, id: 1|name: c+contains: falsefalse2: id: 2|name: java, id: 1|name: c+第8页/共35页第九页,编辑于星期六:十六点 四十九分。public int hashCode() final int PRIME = 31; int result = 1; result = PRIME * result + id;

8、result = PRIME * result + (name = null) ? 0 : name.hashCode(); return result; public boolean equals(Object obj) if (this = obj) return true;if (obj = null) return false;if (getClass() != obj.getClass() return false;final Person other = (Person) obj;if (id != other.id) return false;if (name = null) i

9、f ( != null) return false; else if (!name.equals() return false;return true;lCollection类对象在调用remove、contains 等方法时需要比较对象是否相等,这会涉及到对象类型的 equals 方法和hashCode方法;对于自定义的类型,需要重写equals 和 hashCode 方法以实现自定义的对象相等规则。 l注意:Java中规定,两个内容相同的对象应该具有相等的 hash codes。l 增加Person类的equals和hashCode方法如下:第9页/

10、共35页第十页,编辑于星期六:十六点 四十九分。第10页/共35页第十一页,编辑于星期六:十六点 四十九分。所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象。Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作。Iterator接口定义了如下方法:boolean hasNext(); /判断是否有元素没有被遍历Object next(); /返回游标当前位置的元素并将游标移动到下一个位置void remove(); /删除游标左面的元素,在执行完next之后该 /操作只能执行一次游标游标Next()元素元素第11

11、页/共35页第十二页,编辑于星期六:十六点 四十九分。import java.util.*;public class IteratorTest public static void main(String args) Collection c = new ArrayList(); c.add(new Integer(1); c.add(new Integer(2); c.add(new Integer(3); c.add(new Integer(4); Iterator it = c.iterator(); while (it.hasNext() /next()的返回值为Object类型,需要

12、转换为相应类型Object tem = it.next(); System.out.println (Value() + ); 输出结果:输出结果:1 2 3 4 第12页/共35页第十三页,编辑于星期六:十六点 四十九分。import java.util.*;public class IteratorTest1 public static void main(String args) Collection c = new ArrayList();c.add(good); c.add(morning);c.add(key); c.add(happy);for (Iterator

13、it = c.iterator(); it.hasNext(); ) String tem = (String) it.next();if (tem.trim().length() = 3) it.remove();System.out.println(c);输出结果:输出结果:good, morning, happy第13页/共35页第十四页,编辑于星期六:十六点 四十九分。第14页/共35页第十五页,编辑于星期六:十六点 四十九分。import java.util.*;public class SetTest public static void main (String args) Se

14、t s = new HashSet();s.add (hello);s.add (world);s.add (new Integer(4);s.add (new Double(1.2);s.add (hello); / 相同的元素不会被加入System.out.println (s);输出结果:输出结果:4, hello, 1.2, world第15页/共35页第十六页,编辑于星期六:十六点 四十九分。import java.util.*;public class TreeSetTest public static void main (String args) Set s = new Tre

15、eSet();s.add (new Integer(5); s.add (new Integer(1);s.add (new Integer(4); s.add (new Integer(2);s.add (new Integer(3); s.add (new Integer(4);Iterator it = s.iterator();while (it.hasNext() Integer tem = (Integer) it.next(); System.out.println (Value();输出结果:输出结果:1, 2, 3, 4, 5第16页/共35页第十七页,编辑于星

16、期六:十六点 四十九分。第17页/共35页第十八页,编辑于星期六:十六点 四十九分。void add( Object element);void add (int index, Object element); Object get (int index);Object set (int index,Object element);/修改某一位置的元素 Object remove (int index); int indexOf (Object o);/如果没有该数据,返回-1第18页/共35页第十九页,编辑于星期六:十六点 四十九分。import java.util.*;public cla

17、ss ListTest public static void main(String argc) List l1 = new ArrayList();for (int i = 0; i = 5; i+) l1.add(a + i);System.out.println(l1);list.add(3, a100);System.out.println(l1);list.set(6, a200);System.out.println(list);System.out.print(String) list.get(2) + );System.out.println(list.indexOf(a3);

18、list.remove(1);System.out.println(list);输出结果:输出结果:a0, a1, a2, a3, a4, a5a0, a1, a2, a3, a4, a5a0, a1, a2, a100, a3, a4, a5a0, a1, a2, a100, a3, a4, a5a0, a1, a2, a100, a3, a4, a200a0, a1, a2, a100, a3, a4, a200a2 4a2 4a0, a2, a100, a3, a4, a200a0, a2, a100, a3, a4, a200第19页/共35页第二十页,编辑于星期六:十六点 四十九分。

19、类 java.util.Collections 提供了一些静态方法实现了基于List容器的一些常用算法。void sort(List) /对List容器内的元素排序, /排序的规则是按照升序进行排序。 void shuffle(List) /对List容器内的元素进行随机排列void reverse(List) /对List容器内的元素进行逆续排列 void fill(List, Object) /用一个特定的对象重写整个List容器int binarySearch(List, Object)/对于顺序的List容器,采用折半查找的 /方法查找特定对象第20页/共35页第二十一页,编辑于星期六

20、:十六点 四十九分。输出结果输出结果:a0, a1, a2, a3, a4a1, a4, a2, a3, a0a0, a3, a2, a4, a1a0, a1, a2, a3, a42hello, hello, hello, hello, helloimport java.util.*;public class CollectionsTest public static void main(String argc) List aList = new ArrayList();for (int i = 0; i obj 返回负数表示 this obj实现了Comparable 接口的类通过实现 c

21、omparaTo 方法从而确定该类对象的排序方式。第23页/共35页第二十四页,编辑于星期六:十六点 四十九分。public class Student implements Comparable private String name;private Integer score;public Student(String name, int score) = name;this.score = new Integer(score);public int compareTo(Object o) Student n = (Student) o;int a = pareTo(n

22、.score);return (a != 0 ? a : pareTo();public String toString() return name: + name + score: + score.toString();第24页/共35页第二十五页,编辑于星期六:十六点 四十九分。import java.util.*;public class StudentTest public static void main(String args) List l1 = new LinkedList();l1.add(new Student(“ttt, 66);l1.add(new Stud

23、ent(“bbb, 77);l1.add(new Student(“ccc, 99);l1.add(new Student(“fff, 88);l1.add(new Student(“aaa, 66);System.out.println(l1);Collections.sort(l1);System.out.println(l1);name: ttt score: 66, name: bbb score: 77, name: ccc score: 99, name: fff score: 88, name: aaa score: 66name: aaa score: 66, name: tt

24、t score: 66, name: bbb score: 77, name: fff score: 88, name: ccc score: 99第25页/共35页第二十六页,编辑于星期六:十六点 四十九分。第26页/共35页第二十七页,编辑于星期六:十六点 四十九分。Object put(Object key, Object value);Object get(Object key);Object remove(Object key);boolean containsKey(Object key);boolean containsValue(Object value);int size();boolean isEmpty();void put

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论