21天学通Java学习笔记-Day08(数据结构)

来源:互联网 发布:矩阵的秩的所有性质 编辑:程序博客网 时间:2024/05/22 14:21

Day08:

数据结构【集合 , 容器】(重点)

 

Collection 接口(Collection 层次结构 中的根接口。)  一个一个的储存:

Set  接口(一个不包含重复元素的 collection。)  无序不可重复 (用是否 equals 判断是否重复)

List  接口 (有序的 collection(也称为序列)。) 有序可以重复 (用是否 equals 判断是否重复)

 

如果要让两个或者多个对象相等(实现自定义对象相等规则),就要重写 equals方法 和 hashCode方法。(重写equals( )方法就一定要重写hashCode( )方法)

 

Map  接口 (将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。

 ) 以 键值对 形式存储

 

Iterator<E> 接口  (对 collection 进行迭代的迭代器。) 所有实现Collection 接口的集合(容器)类都有一个 iterator( )方法用以返回一个实现了 Iterator 接口的对象。

    Iterator 接口的方法:

         boolean    hasNext( )   如果仍有元素可以迭代则返回 true 

          E             next( )         返回迭代的下一个元素。

         void          remove( )   从迭代器指向的collection中移除迭代器返回的最后一个元素。

Iterator 对象的 remove( )方法时在迭代过程中删除元素的唯一安全方法。

Iterator 接口的方法使用一:

   

 Collection c = new HashSet( );    Iterator  i = c.iterator( );    while(i.hasNext( )) {        Dog d = (Dog)i.next( );  //next( )的返回值是Object类型,需要转换相应的类型     }

 

Iterator 接口的方法使用二:

    

Collection c = new HashSet( );    for(Iterator i = c.iterator( ); i.hasNext( ); ) {        Dog d = (Dog)i.next( );        if( d.getFirstName( ).length( )<3) {            i.remove( );  //Iterator 对象在迭代的时候会进行锁定元素,只能用Iterator对象的remove( )方法进行删除,使用Collection对象的remove( )方法会出错。        }    }

 

JDK1.5增强的for循环:

例子代码:

import java.util.*;public class JQFor { public static void main(String[]args){  int[] arr = {1,2,3,4,5,6,7,8,9};  for(int i:arr) {   System.out.println(i);  }   Collection c = new ArrayList();  c.add(new String("aaa"));  c.add(new String("bbb"));  c.add(new String("ccc"));  for(Object b:c) {   System.out.println(b);  } }}


增强for循环的缺陷:

    数组 缺陷:不方便访问下标值。

    集合 缺陷:与Iterator相比,不方便删除集合中的内容(增强for循环内部也是调用Iterator)

 

除了简单遍历并且读出内容外,不建议使用增强for循环

 

Set 接口:

    Set 接口是 Collection 的子接口,Set接口没有提供额外的方法,实现Set接口的集合(容器)类中的元素无顺序,不可重复。

 

Set 集合(容器)类有 HashSet , TreeSet 等。

 

添加的方法:add(object b);

 

List 接口:

    List 接口是 Collection 的子接口,实现 List接口的容器(集合)类中的元素有顺序,可以重复。

    List 容器(集合)中的元素都对应一个整数型的序号记载其在容器(集合)中的位置,可以根据序号存取容器中的元素。

 

List 集合(容器)类有ArrayList(底层数组实现) , LinkedList(底层链表实现) 等。

 

添加的方法:add(object b);

 

List 算法:

    类 java.util.Collections 提供一些静态方法实现了 List 集合(容器)的一些常用算法。

 

常用的方法:

    void sort(List)            对 List 容器内的元素排序

    void shuffle(List)        对 List 容器内的对象进行随机排序

    void reverse(List)        对 List 容器内的对象进行逆序排序

    void fill(List , Object)        用一个特定的对象重写整个 List 容器

    void copy(List s ,  List t)        将 t List 容器内容拷贝到 s List 容器

    int binarySearch(List , Object)        对顺序 List 容器,采用折半法(二分法)查找的方法查找特定对象   

Collections 类的方法例子代码:

List l1 = new LinkedList( );

for(int i = 0; i<=9; i++) {

    l1.add("a"+i);

}

Collections.shuffle( l1 );    //随机排序

Collections.reverse( l1 );    //逆序排列

Collections.sort( l1 );    //顺序排序

Collentions.binarySearch( l1, "a5" );    //用折半查找法(二分法),在 l1 中查找 "a5" 元素

 

Comparable 接口:

sort(List) 

shuffle(List)

reverse(List)

这些算法是根据什么判断容器中对象的“大小”顺序?

所有可以“排序”的类都实现了 java.lang.Comparable 接口,Comparable 接口只有一个方法

public int compareTo(T o);

该方法:返回 0 表示 this == o;

              返回 正数 表示 this > o;

              返回 负数 表示 this < o;

实现 Comparable 接口的 compareTO 方法可以确定对象的排序方式

Comparable接口的compareTo方法例子代码:

import java.util.*;public class testcom { public static void main(String[]args) {  List l = new LinkedList();  l.add(new Name("Karl","M"));  l.add(new Name("John","O"));  l.add(new Name("Tom","M"));  Collections.sort(l);  System.out.println(l);  Collections.reverse(l);  System.out.println(l); } } class Name implements Comparable { private String firstName,lastName; public Name(String firstName, String lastName) {        this.firstName = firstName;        this.lastName = lastName;    }   public String toString() {          return firstName + " " + lastName;     }   public int compareTo(Object o) {        Name n = (Name)o;        int lastCmp =  lastName.compareTo(n.lastName);         return             (lastCmp!=0 ? lastCmp :  firstName.compareTo(n.firstName));     }}


 

实现 Comparable 接口的 compareTO 方法的对象类型被当做元素存储到List中以后就可以调用sort(List),shuffle(List),reverse(List)来进行排序

 

Map 接口:

    实现 map 接口的类用来存储键值对。

    Map类中存储的键值对通过键来标识,所以键不能重复。

    map 解耦的实现类有HashMap , TreeMap 等。

 

添加的方法:put(object key , object value);

 

自动打包,自动解包:

    自动打包:自动将基础类型转化为对象

    自动解包:自动将对象转化为基础类型

import java.util.*;public class TestMap {  public static void main(String args[]) {Map m1 = new HashMap();    Map m2 = new TreeMap();//m1.put("one",new Integer(1));    m1.put("one", 1);     //自动打包,将 int 类型自动打包成 Integer 对象//m1.put("two",new Integer(2));    m1.put("two", 2); //m1.put("three",new Integer(3));    m1.put("three", 3);//m2.put("A",new Integer(1));    m2.put("A", 1);   //m2.put("B",new Integer(2));    m2.put("B", 2);System.out.println(m1.size());    System.out.println(m1.containsKey("one"));    System.out.println        //(m2.containsValue(new Integer(1)));        (m2.containsValue(1));     if(m1.containsKey("two")) {      //int i = ((Integer)m1.get("two")).intValue();      int i = (Integer)m1.get("two");  //自动解包,将 Integer 对象自动解包为 int 类型      System.out.println(i);    }    Map m3 = new HashMap(m1);    m3.putAll(m2);    System.out.println(m3);  }}


泛型:

import java.util.*;public class BasicGeneric { public static void main(String[] args) {  List<String> c = new ArrayList<String>();  //添加泛型 String  c.add("aaa");  c.add("bbb");  c.add("ccc");  for(int i=0; i<c.size(); i++) {   String s = c.get(i);    //取出来时不需要再强制转换   System.out.println(s);  }  Collection<String> c2 = new HashSet<String>();  c2.add("aaa");   c2.add("bbb");  c2.add("ccc");  for(Iterator<String> it = c2.iterator(); it.hasNext(); ) {   String s = it.next();  //取出来时不需要再强制转换   System.out.println(s);  } }}class MyName implements Comparable<MyName> {    //只和MyName对象进行比较 int age; public int compareTo(MyName mn) {     //泛型规定什么类型,方法参数就是什么类型  if(this.age > mn.age) return 1;  else if(this.age < mn.age) return -1;  else return 0; }}


 

JAVA API 文档中的类,接口后面有< >,就可以用泛型

0 0
原创粉丝点击