---
title: "java的基本语法(四)"
author: "cfeers"
date: "2010-09-11T16:45:31Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2010-09-11T16:45:31Z"
id: 760956
url: "https://cfeers.iteye.com/blog/760956"
---

**自己记得一些笔记：容器**

【1-1.容器的感念】：  
Java API 所提供的一系列类的实例，用于在程序中存放对象。

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
【1-2.容器API】  
J2SDK 所提供的容器API 位于java.util包内  
容器API的类图机构如下：

      Collection                           
Set                List                  Map  
HashSet    LinkedList  ArrayList        HashMap     
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
\*Set (没有顺序不可以重复)   
\*List(有顺序,可以重复&lt;equals&gt;)  
\*Map(存储"键(key)——值(value)映射对"的方法,2个2个往里装)  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
\*如何选择数据结构*  
衡量标准：读的效率和改的效率

\* Array读快改慢  
\* Linked改快读慢  
\* Hash两者之间  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
【1-3.Collection接口】  
Collection接口中所定义的方法：  
int size();          放了多少个元素  
boolean isEmpty();   是否为空  
void clear();        清空  
boolean contains(Object element);  是不是包含指定的某个对象元素(equals)  
boolean add(Object element);       添加  
boolean remove(Object element);    去除  
Iterator iterator();  
boolean containsAll(Collection c);  是不是包含所有元素  
boolean addAll(Collection c);     
boolean removeAll(Collection c);  
boolean retainAll(Collection c);   集合类的交集  
Object\[] toArray();                 里面的对象转换为一个对象类型的数组

\------------------------------------------------------------------------  
容器类对象在调用remove、contains等方法时需要比较对象是否相等，这会涉及到  
对象类型的equals方法 和hashCode方法；对于自定义的类型，需要要重写equals  
和hashCode方法以实现自定义的对象相等规则。  
【注意】：相等的对象应该具有相等的hash codes。  
例子如下：  
import java.util.\*;  
public class Test{  
 public static void main(String\[] args){  
  Collection c = new HashSet();  
  c.add("Hello");  
         c.add(new Name("f1","l1"));  
  c.add(new Integer(100));  
  c.remove("Hello");  
  c.remove(new Integer(100));  
  System.out.println(c.remove(new Name("f1","l1")));  
  System.out.println(c);   
 }   
}

 class Name{  
 private String firstName,lastName;  
 public Name(String firstName, String lastName){  
  this.firstName = firstName;  
  this.lastName = lastName;   
 }  
 public String getFirstName(){  
  return firstName;   
 }  
 public String getLastName(){  
  return lastName;   
 }  
  public String toString(){  
   return firstName + " " + lastName;    
  }

public boolean equals(Object obj){  
 if(obj instanceof Name){  
   Name name = (Name) obj;  
   return (firstName.equals(name.firstName))&&(lastName.equals(name.lastName));   
 }    
 return super.equals(obj);  
}

public int hashCode(){  
 return firstName.hashCode();   
}  
}  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
【1-4.lterator接口】  ====&gt; 就是一个统一的，来遍历Collection里面所有元素的方法，

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

所有实现了Collection 接口的容器类都有一个iterator方法用以返回一个  
实现了Iterator接口的对象  
Iterator对象称作迭代器，用以方便的实现对容器内元素的遍历操作  
Iterator接口定义了如下方法：  
   boolean hasNext(); //判断游标右边是否有元素  
   Object next();     //返回游标右边的元素并将游标移动到下一个位置  
   void remove();     //删除游标左面的元素，在执行完next之后，该操作只能执行一次  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
【1-5.增强的for循环】  
JDK 1.5增强的for循环，

增强的for循环对于遍历array 或 Collection 的时候相当简单

缺陷：  
    数组：不能方便的访问下标值  
    集合：与使用Iterator(可以调用remove方法)相比，不能方便的删除集合中的内容  
            在内部也是调用Iterator  
总结：除了简单遍历并读出其中的内容外，不建议使用增强for循环

例如：

import java.util.\*;  
public class EnhancedFor{  
  public static void main(String\[] args){  
     int\[] arr = {1,2,3};  
     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 o : c){  
       System.out.println(o);   
     }  
  }  
}

result：  
1  
2  
3  
aaa  
bbb  
ccc  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
【1-6.Set接口】

Set接口是Collection 的子接口，Set接口没有提供额外的方法，但实现  
Set接口的容器类中的元素是没有有顺序的，而且不可以重复。

Set容器可以与数学中的“集合”的概念相对应

J2SDK API中所提供的Set容器类有HashSet，TreeSet等  
例如：  
import java.util.\*;  
public class TestSet{  
 public static void main(String\[] args){  
  Set s1 = new HashSet();  
  Set s2 = new HashSet();  
  s1.add("a");s1.add("b");s1.add("c");   
  s2.add("d");s2.add("a");s2.add("b");  
   //构造方法 用以初始化容器类   
   Set sn = new HashSet(s1);  
   sn.retainAll(s2);  
   Set su = new HashSet(s1);  
   su.addAll(s2);  
   System.out.println(sn);  
   System.out.println(su);  
 }   
}  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
【1-7.List接口 和 Comparable接口】

            ======1.List接口======

List接口是Collection的子接口，实现List接口的容器类中的元素是有顺序的，而且可以重复  
List容器中的元素都对应一个整数型的序号记载其在容器中的位置，可以根据序号存取容器中的元素。  
J2SDK所提供的List容器类有ArrayList，LinkedList等

   【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 dest,List src) 将src List容器内容拷贝到dest List容器  
 int binarySearch(List, Object)对于顺序的List容器，采用折半查找的方法查找特定的对象

            =====2.Comparable接口=====

所有可以"排序"的类都实现了Java.lang.Comparable 接口,Comparable接口  
中只有一个方法：  
   public int CompareTo(Object obj); 该方法：

  返回0 表示 this == obj  
  返回正数表示 this &gt; obj  
  返回负数表示 this &lt; obj

实现了Comparable接口的类通过实现 comparaTo 方法从而确定该类对象的排序方式。  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
【1-8.Map 接口】

实现Map接口的类用来存储 键—&gt;值 对。  
Map接口的实现类有HashMap(Hash索引)和TreeMap(Tree索引)等。  
Map类中存储的键—&gt;值对通过键来标识，所以键值不能重复(互相必须equals，但是效率太低，所以要用hashCode，返回值int类型)。

 【在JDK1.5以后】可以直接这样写：  
Map m1 = new HashMap();  
m1.put("one",1); 

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
【1-9.自动打包/解包】

Auto-boxing/unboxing

打包：自动将基础类型转换为对象  
解包：自动将对象转换为基础类型

\*\*\*\*\*\*\*\*\*\*\*\*\*没有打包的程序\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**   
Map m1 = new HashMap();  
  Map m2 = new TreeMap();   
  m1.put("one",new Integer(1));  
  m1.put("two",new Integer(2));  
  m1.put("three",new Integer(3));  
  m2.put("A",new Integer(1));  
  m2.put("B",new Integer(2));  
  System.out.println(m1.size());  
  System.out.println(m1.containsKey("one"));  
  System.out.println(m2.containsValue(new Integer(1)));  
  if(m1.containsKey("two")){  
   int i =((Integer)m1.get("two")).intValue();  
   System.out.println(i);  
  }  
  Map m3 = new HashMap(m1);  
  m3.putAll(m2);  
  System.out.println(m3);

 

\*\*\*\*\*\*\*\*\*\*\*\*\*自动打包以后的程序（泛型）\*\*\*\*\*\*\*\*\*\*\*\*\**  
import java.util.\*;  
public class TestMap2{  
 public static void main(String\[] args){  
  Map m1 = new HashMap();  
  Map m2 = new TreeMap();   
  m1.put("one",1);  
  m1.put("two",2);  
  m1.put("three",3);  
  m2.put("A",1);  
  m2.put("B",2);  
  System.out.println(m1.size());  
  System.out.println(m1.containsKey("one"));  
  System.out.println(m2.containsValue(1));  
  if(m1.containsKey("two")){  
   int i =(Integer)m1.get("two");  
   System.out.println(i);  
  }  
  Map m3 = new HashMap(m1);  
  m3.putAll(m2);  
  System.out.println(m3);  
 }   
}

【1-10.泛型(JDK1.5新增)】  
可以在定义Collection的时候指定  
也可以再循环时用Iterator指定

泛型：增加程序的可读性和稳定性

import java.util.\*;  
public class BasicGeneric{  
 public static void main(String\[] args){  
  List&lt;String&gt; c = new ArrayList&lt;String&gt;();  
  c.add("aaa");  
  c.add("bbb");  
  c.add("ccc");  
  for(int i=0; i&lt;c.size(); i++){  
   String s = c.get(i);  
   System.out.println(s);   
  }   
    
  Collection&lt;String&gt; c2 = new HashSet&lt;String&gt;();  
  c2.add("aaa");c2.add("bbb");c2.add("ccc");  
  for(Iterator&lt;String&gt; it = c2.iterator(); it.hasNext();){  
   String s = it.next();  
   System.out.println(s);   
  }  
 }   
}

\-----------------------------------------  
import java.util.\*;  
public class TestMap2{  
 public static void main(String\[] args){  
  Map&lt;String,Integer&gt; m1 = new HashMap&lt;String,Integer&gt;();  
    
  m1.put("one",1);  
  m1.put("two",2);  
  m1.put("three",3);  
   
  System.out.println(m1.size());  
  System.out.println(m1.containsKey("one"));  
   
  if(m1.containsKey("two")){  
   //int i =(Integer)m1.get("two");  
   int i = m1.get("two");  
   System.out.println(i);  
  }  
    
 }   
}

=====================总结第七章=========================

一个图，  
一个类(Collection)，  
三个知识点：For  Generic(泛型)  Auto-boxing/unboxing，  
六个接口     
      Collection                           
Set                List                  Map  
HashSet    LinkedList  ArrayList        HashMap 

lterator接口  ====&gt; 就是一个统一的，来遍历Collection(集合)里面所有元素的方法