【java数据结构】

一、List接口,有序的Collection接口,能够精确地控制每个元素插入的位置,允许有相同的元素

  1.链表,LinkedList实现了List接口,允许null元素,提供了get()、remove()、insert()方法,没有同步方法

  [java] view plaincopy

  public void add() {

  LinkedList List = new LinkedList();

  List.add("link1");

  List.add("link2");

  List.add("link3");

  Iterator it = List.iterator();

  while (it.hasNext()) {

  System.out.println(it.next());

  }

  it.remove();

  Iterator it1 = List.iterator();

  for (int i = 0; i < List.size(); i++) {

  System.out.println(it1.next());

  }

  }

  2.数组列表,ArrayList,可以动态变化容量的数组,非同步的

  数组列表中存放的是Object类型,因此在数组列表中存放的对象类型,以其原型的父类代替,提取其中的元素时要进行类型转换

  [java] view plaincopy

  public static void main(String[] args)

  {

  ArrayList al=new ArrayList();

  al.add("name");

  al.add("value");

  al.add("number");

  for(int i=0;i<al.size();i++)

  {

  System.out.println(al.get(i));

  }

  }

  二、Set接口,不包含重复元素的Collection接口

  1.散列集,HashSet,实现了Set接口,非线性同步

  与链表和数组列表几乎类似,但在数据处理时,比使用链表进行数据处理花费时间更短,处理大数据时通常使用散列集

  [java] view plaincopy

  public static void main(String[] args)

  {

  long time=0;

  HashSet hs=new HashSet();

  ArrayList al=new ArrayList();

  long starttime=System.currentTimeMillis();

  for(int i=0;i<10000;i++)

  {

  hs.add(new Integer(i));

  }

  System.out.println(System.currentTimeMillis()-starttime);

  for(int i=0;i<10000;i++)

  {

  al.add(new Integer(i));

  }

  System.out.println(System.currentTimeMillis()-starttime);

  }更多精彩教程请关注:windows7系统下载

posted on 2013-08-07 09:44  潇洒kman  阅读(115)  评论(0编辑  收藏  举报