Java数据结构----集合

Java的集合可以分为两类,

第一类是以数组为代表,这类集合可以描述线性表类型的数据结构,以Collection为基类,其中自己用过或者了解的有

实现List接口的类:LinkedList,ArrayList,Vector,Stack...

实现Set接口的类:TreeSet,HashSet...

第二类是以Map为代表的“键值对”类型的数据结构,以Map为基类,其中有

HashMap,HashTable,Tree

第一类

Vector

Vector与数组相比,Vector对象可以很好的实现元素的插入和删除,也拥有动态增长特性

1.构造函数(4)

  • Vector()
  • Vector(<E> c)使用泛型(generic type)确定容纳对象的类型
  • Vector(int initialCapacity,int capacityIncrement)

  能为初始化的Vector对象分配长度为initialCapacity的容量,可以在必要的时候以capacityIncrement的速度自增长其容量空间

  • Vector(int initialCapacity)

2.常用方法

  • addElement(E obj)向Vector中添加元素

  通过这个方法可把obj对象添加到该Vector对象的尾部,同时Vector的size加1。

  • insertElementAt(E obj)在指定索引处添加元素

  通过这个方法,可以把obj对象添加到参数指定的index索引处,此后的Vector对象里的各内容自动向后移动一个单位

  setElement(E obj,int index)替换指定位置的元素

  • boolean removeElement(Object obj)删除Vector对象中的第一个obj对象,返回一个bool类型的值用来表示是否找到并删除指定对象
  • void removeElementAt(int index)删除指定位置的元素
  • void removeAllElements()删除Vector对象中的所有元素,size置为0
  • int size()获得Vector当前长度

 

 1 /*
 2  *@author SimonKly E-mail:deGaulleKong@gmail.com
 3  *@version:2017-2-25
 4  */
 5 import java.util.Vector;
 6 public class VectorTest {
 7     public static void main(String[] args) {
 8 //        实例一个Vector对象
 9         Vector v=new Vector();
10 //        向Vector对象中添加元素
11         v.addElement(new Integer(1));
12         v.addElement("two");
13         v.addElement("Three");
14 //        向Vector对象指定位置插入
15         v.insertElementAt(0, 0);
16 //        替换指定位置的元素
17         v.setElementAt("four",3);
18 //        遍历Vector对象
19         System.out.print("element :");        
20             for(int i=0;i<v.size();i++){
21                 if(v.elementAt(i) instanceof Integer)//判断元素类型
22                     System.out.print(" "+((Integer)v.elementAt(i)));
23                 else
24                     System.out.print(" "+(String)v.elementAt(i));
25             }
26         
27         System.out.println();
28 //        删除指定位置的元素
29         v.removeElementAt(3);
30         System.out.print("element :");
31 //        遍历验证
32         for(int i=0;i<v.size();i++){
33             if(v.elementAt(i) instanceof Integer)
34                 System.out.print(" "+((Integer)v.elementAt(i)));
35             else
36                 System.out.print(" "+(String)v.elementAt(i));
37         }
38         System.out.println();
39 //        删除所有元素
40         v.removeAllElements();
41         if(v.size()==0){
42             System.out.println("Cleared!");
43         }
44     }
45 }

 

 

 

Stack

Stack继承了Vector,Stack重用 了Vector的存储对象空间和访问线性表方法,而Stack先进后出的特性可以看作Vector的特例

1.构造函数

  • Stack()
  • Stack(<E s>)

2.常用方法

  • E push(E item)向堆栈中压入item,并将item对象返回
  • E peek()返回指定栈顶元素的类型
  • E pop()弹出栈顶元素
  • boolean empty()判断堆栈是否为空

 

 1 package $1;
 2 
 3 /**
 4 *@author: SimonKly
 5 *@E-mail:deGaulleKong@gmail.com
 6 *@date: 2017年2月27日
 7 */
 8 
 9 import java.util.Iterator;
10 import java.util.Stack;
11 
12 public class StackTest {
13 
14     public static void main(String[] args) {
15         Stack s=new Stack();
16         s.push("one");
17         s.push("two");
18         s.push("three");
19         Iterator iterator=s.iterator();
20         System.out.println("iterator:");
21         while(iterator.hasNext()){
22             System.out.println(iterator.next());
23         }
24         System.out.println("normal:");
25         for(int i=0;i<s.size();i++){
26             System.out.println(s.get(i));
27         }
28         System.out.println("pop");
29         while(!s.isEmpty()){
30             System.out.println(s.pop());
31         }
32         //由于继承了Vector则,不好用法则会Stack把当成Vector
33         s.addElement("badusage1");
34         s.addElement("badusage2");
35         s.addElement("badusage3");
36         for(int i=0;i<s.size();i++){
37             System.out.println(s.elementAt(i));
38         }
39     }
40 
41 }

 

 

 

List接口

Vector和Stack都实现了List接口

其中的方法

  • 插入

    void add(int index,E element)在索引号index后插入element元素

    boolean add(E o)直接插入到链表的最后

  • 删除

    E remove(int index)删除链表中指定的位置

    boolean remove(Object o)删除链表中第一个元素

  • 获取元素

    E get(int index)获取指定位置的元素

int size()统计有多少元素

int indexOf(Object obj)获取obj对象的索引位置

List<E>subList(int fromIndex,int to Index)截取链表,得到链表里的从fromindex开始到toIndex结束的子链表

void clear()清空链表

LinkedList实现了List

1.构造函数

LinkedList()

LinkedList(Collection c)

LinkedList(<E> c)

2.添加元素

void addFirst(E obj)//添加头元素

void addLast(E obj)//添加尾元素

3.获取元素

E getFirst()//获取第一个

E getLast()//获取最后一个

4.删除元素

E removeFirst()//删除第一个并返回第一个元素

E removeLast()//删除最后一个并返回最后一个元素

 

 1 package $1;
 2 
 3 /**
 4 *@author: SimonKly
 5 *@E-mail:degaullekong@gmail.com
 6 *@date: 2017年2月27日
 7 */
 8 import java.util.Iterator;
 9 import java.util.LinkedList;
10 import java.util.List;
11 public class LinkedListTest {
12 
13     public static void main(String[] args) {
14 //        使用泛型实例化一个LinkedList对象
15         List<String>ll=new LinkedList<String>();
16 //    向LinkedList中添加元素
17         ll.add("one");
18         ll.add("two");
19         ll.add("three");
20 //        不使用泛型实例化一个LinkedList对象
21         List l=new LinkedList();
22         l.add("first");
23         l.add("second");
24         l.add(3);//可以放入不同的数据类型
25 //        打印指定位置的元素
26         System.out.println("the index of 3:"+l.indexOf(3));
27 //        移除指定位置的元素
28         ll.remove(2);
29 //        使用Iterator对象遍历
30         Iterator iterator=ll.iterator();
31         while(iterator.hasNext()){
32             System.out.println(iterator.next());
33         }
34 //        使用一般遍历方法遍历
35         for(int index=0;index<l.size();index++){
36             System.out.println(l.get(index));
37         }    
38     }
39 }

 

不允许有重复元素的Set接口

接口中的方法:

1.添加元素:boolean add(E o)用到泛型,如果待插入的元素不在Set中则返回true,反之,返回false

2.删除指定元素:boolean remove(Object o)如果找到并成功删除则返回true ,反之,返回false

3.判断Set是否为空:boolean isEmpty()

4.返回Set大小的Size();

实现的类有HashSet,TreeSet(输出是有序)

第二类

 HashTable

1.构造函数

HashTable()//初始容量为11,默认装载因子0.75

HashTable(int initialCapacity)//初始化容量,默认装载因子0.75

HashTable(int initialCapacity,float loadFactor)//初始化容量和装载因子

HashTable(<K,V>t)//使用泛型

2.添加键值

put(K key ,V value)

3.根据键值获取值

V get(Object key)

4.判断“键”和“值”是否存在

boolean containsKey(Object key)

boolean containsValue(Object value)

封装散列表数据结构的Map接口,实现的类:HashMap和TreeMap

其中方法:

1.V put(K key,V value)插入键值对,并返回value

2.V get(Object key)

3.boolean containsKey(Object key) boolean containsValue(Object value)判断是否存在键或者值

4.Set<Map.Entry<K,V>> entrySet()//返回键值对映射关系,用于遍历

 1 package $2;
 2 /**
 3 *@author: SimonKly
 4 *@E-mail:degaullekong@gmail.com
 5 *@date: 2017年2月27日
 6 */
 7 import java.util.HashMap;
 8 import java.util.Iterator;
 9 import java.util.Map;
10 import java.util.Map.Entry;
11 import java.util.TreeMap;
12 
13 public class MapTest {
14     static void prt(Map m)
15     {
16         Iterator i=m.keySet().iterator();
17         while(i.hasNext()){
18             Object o=i.next();
19             System.out.println("key="+o+",value="+m.get(o));
20         }
21     }
22     public static void main(String[] args) {
23         Map m;
24             
25         System.out.println("HashMap例子:");
26         m=new HashMap();
27         m.put(1, "one");
28         m.put(2, "two");
29         m.put(3, "three");
30         m.put(4, "four");
31         MapTest.prt(m);
32         
33         System.out.println("TreeMap例子:");
34         m=new TreeMap();
35         m.put(3, "three");
36         m.put(4,"four");
37         m.put(1, "one");
38         m.put(2, "two");
39         MapTest.prt(m);
40 //    使用Entry对象遍历Map
41         System.out.println("Entry对象遍历Map:");
42         HashMap<Integer,String> em=new HashMap<Integer,String>();
43         em.put(3, "three");
44         em.put(4,"four");
45         em.put(1, "one");
46         em.put(2, "two");
47 //        使用Entry对象遍历
48         for(Entry<Integer,String> entry:em.entrySet())
49         {
50             System.out.println("key="+entry.getKey()+",value="+entry.getValue());
51         }
52     }
53 }

 

posted @ 2017-02-22 21:04  SimonKly  阅读(223)  评论(0编辑  收藏  举报