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当前长度
 /*
*@author SimonKly E-mail:deGaulleKong@gmail.com
*@version:2017-2-25
*/
import java.util.Vector;
public class VectorTest {
public static void main(String[] args) {
// 实例一个Vector对象
Vector v=new Vector();
// 向Vector对象中添加元素
v.addElement(new Integer(1));
v.addElement("two");
v.addElement("Three");
// 向Vector对象指定位置插入
v.insertElementAt(0, 0);
// 替换指定位置的元素
v.setElementAt("four",3);
// 遍历Vector对象
System.out.print("element :");
for(int i=0;i<v.size();i++){
if(v.elementAt(i) instanceof Integer)//判断元素类型
System.out.print(" "+((Integer)v.elementAt(i)));
else
System.out.print(" "+(String)v.elementAt(i));
} System.out.println();
// 删除指定位置的元素
v.removeElementAt(3);
System.out.print("element :");
// 遍历验证
for(int i=0;i<v.size();i++){
if(v.elementAt(i) instanceof Integer)
System.out.print(" "+((Integer)v.elementAt(i)));
else
System.out.print(" "+(String)v.elementAt(i));
}
System.out.println();
// 删除所有元素
v.removeAllElements();
if(v.size()==0){
System.out.println("Cleared!");
}
}
}

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()判断堆栈是否为空
 package $1;

 /**
*@author: SimonKly
*@E-mail:deGaulleKong@gmail.com
*@date: 2017年2月27日
*/ import java.util.Iterator;
import java.util.Stack; public class StackTest { public static void main(String[] args) {
Stack s=new Stack();
s.push("one");
s.push("two");
s.push("three");
Iterator iterator=s.iterator();
System.out.println("iterator:");
while(iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("normal:");
for(int i=0;i<s.size();i++){
System.out.println(s.get(i));
}
System.out.println("pop");
while(!s.isEmpty()){
System.out.println(s.pop());
}
//由于继承了Vector则,不好用法则会Stack把当成Vector
s.addElement("badusage1");
s.addElement("badusage2");
s.addElement("badusage3");
for(int i=0;i<s.size();i++){
System.out.println(s.elementAt(i));
}
} }

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()//删除最后一个并返回最后一个元素

 package $1;

 /**
*@author: SimonKly
*@E-mail:degaullekong@gmail.com
*@date: 2017年2月27日
*/
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class LinkedListTest { public static void main(String[] args) {
// 使用泛型实例化一个LinkedList对象
List<String>ll=new LinkedList<String>();
// 向LinkedList中添加元素
ll.add("one");
ll.add("two");
ll.add("three");
// 不使用泛型实例化一个LinkedList对象
List l=new LinkedList();
l.add("first");
l.add("second");
l.add(3);//可以放入不同的数据类型
// 打印指定位置的元素
System.out.println("the index of 3:"+l.indexOf(3));
// 移除指定位置的元素
ll.remove(2);
// 使用Iterator对象遍历
Iterator iterator=ll.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
// 使用一般遍历方法遍历
for(int index=0;index<l.size();index++){
System.out.println(l.get(index));
}
}
}

不允许有重复元素的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()//返回键值对映射关系,用于遍历

 package $2;
/**
*@author: SimonKly
*@E-mail:degaullekong@gmail.com
*@date: 2017年2月27日
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap; public class MapTest {
static void prt(Map m)
{
Iterator i=m.keySet().iterator();
while(i.hasNext()){
Object o=i.next();
System.out.println("key="+o+",value="+m.get(o));
}
}
public static void main(String[] args) {
Map m; System.out.println("HashMap例子:");
m=new HashMap();
m.put(1, "one");
m.put(2, "two");
m.put(3, "three");
m.put(4, "four");
MapTest.prt(m); System.out.println("TreeMap例子:");
m=new TreeMap();
m.put(3, "three");
m.put(4,"four");
m.put(1, "one");
m.put(2, "two");
MapTest.prt(m);
// 使用Entry对象遍历Map
System.out.println("Entry对象遍历Map:");
HashMap<Integer,String> em=new HashMap<Integer,String>();
em.put(3, "three");
em.put(4,"four");
em.put(1, "one");
em.put(2, "two");
// 使用Entry对象遍历
for(Entry<Integer,String> entry:em.entrySet())
{
System.out.println("key="+entry.getKey()+",value="+entry.getValue());
}
}
}

最新文章

  1. mysql 工具箱
  2. 燕十八MySQL优化学习笔记
  3. c# 与 PHP中 SHA1加密结果不同解决方法
  4. Eclipse--Team--SVN--URL修改
  5. eclipse手动添加源码
  6. oracle scn浅析
  7. latex使用笔记
  8. 源码剖析Django REST framework的请求生命周期
  9. tmux 终端复用详解
  10. [BZOJ]1019 汉诺塔(SHOI2008)
  11. 关于Tomcat的URIEncoding以及GET乱码
  12. 代理与hook
  13. 深入并发包 ConcurrentHashMap 源码解析
  14. servlet实现简单的登录功能
  15. 异常--finally关键字
  16. css预编译语言sass——mixin的使用
  17. 性能测试day04_性能监控
  18. iOS 创建一个App目录结构
  19. Atitit vue.js 把ajax数据 绑定到form表单
  20. 【Foreign】tty的方程math [数学]

热门文章

  1. 给Android 应用开发者的十个建议(转)
  2. 75、python学习第一篇
  3. 基于MFC的Media Player播放器的制作(1---播放器界面的布局)
  4. .NET简介
  5. 20140922 tcpip3次握手 分段 分页 spooling 位示图
  6. c# dotNetBar symbol属性代码动态设置方法
  7. python获取港股通每日成交信息
  8. zabbix主动模式设置
  9. CG-CTF misc部分wp
  10. 2018APIO 进京赶考