现在的位置: 首页 > 综合 > 正文

java基础_Day14

2017年09月30日 ⁄ 综合 ⁄ 共 14095字 ⁄ 字号 评论关闭

1,集合框架:
     1,集合:存储对象的容器。
     2,集合框架:
          Collection
               ----List
                         ----ArrayList//底层是数组数据结构,查询速度快,增删速度慢(元素越多越慢)。线程不同步,JKD1.2以后替代了Vector//默认长度为10超过了50%延长变为15
                         ----LinkedList//底层是链表数据结构,增删速度快,查询速度慢,线程不同步
                         ----Vector//底层是数组数据结构。线程同步。增删,查询都慢
               ----Set
                         -----HasSet//底层是哈希表数据结构,元素是无序的,即存入和取出的顺序不一定一致。
                         -----TreeSet
               ----Map
                         -----HasTable
                         -----HasMap
                         -----TreeMap
     3,集合与数组的区别:
              数组:长度固定,数组既可以存储对象,也可以存储基本数据类型,集合中只能存储同一种类型的数据
              集合:长度可变,可以用于存储不容类型的对象
     4,为什么会出现这么多容器:
              因为每一种容器对数据的存储方式都不相同,这种存储方式叫做数据结构

     5,集合的共性方法:

     add(Objcet o):参数是Obcjet类型的,便于接收任意类型的对象
     集合中存储的是对象的引用(地址)
     boolean add(E e)
                 确保此 collection 包含指定的元素(可选操作)。
     boolean addAll(Collection<? extends E> c)
                 将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
     void clear()
                 移除此 collection 中的所有元素(可选操作)。
     boolean contains(Object o)
                 如果此 collection 包含指定的元素,则返回 true。
     boolean containsAll(Collection<?> c)
                 如果此 collection 包含指定 collection 中的所有元素,则返回 true。
     boolean equals(Object o)
                 比较此 collection 与指定对象是否相等。
     int hashCode()
                 返回此 collection 的哈希码值。
     boolean isEmpty()
                 如果此 collection 不包含元素,则返回 true。
     Iterator<E> iterator()
                 返回在此 collection 的元素上进行迭代的迭代器。
     boolean remove(Object o)
                 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
     boolean removeAll(Collection<?> c)
                 移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
     boolean retainAll(Collection<?> c)
                 仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。
     int size()
                 返回此 collection 中的元素数。
     Object[] toArray()
                 返回包含此 collection 中所有元素的数组。
     <T> T[]
     toArray(T[] a)
          返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。

      6,eg:

import java.util.*;
class  CollectionDemo
{
     public static void sop(Object obj)
     {
          System.out.println(obj);
     }
     public static void method_1()
     {
          Collection al = new ArrayList();
          al.add("java01");
          al.add("java02");
          al.add("java03");
          al.add("java04");
          Collection al2 = new ArrayList();
          al2.add("java04");
          al2.add("hehe");
          al.addAll(al2);
          sop(al.contains("java04"));//true
          sop(al.containsAll(al2));//true
          sop(al.hashCode());
//          al.clear();//[]
          sop(al);//[java01, java02, java03, java04]
     }
     public static void main(String[] args)
     {
          method_1();
     }
}

2,迭代器:

概念与原理:
          就是集合中取出元素的方式。
          这种取出方式定义在集合内部。被定义成了内部类,抽取容器功能的规则定义成了Iterater接口,集合内部提供了取出集合中对象的方法:iterater()
eg:


import java.util.*;
class IteratorDemo
{
     public static void sop(Object obj)
     {
          System.out.println(obj);
     }
    
     public static void main(String[] args)
     {
          ArrayList al =new ArrayList();
          al.add("java01");
          al.add("java02");
          al.add("java03");
          al.add("java04");


          Iterator it = al.iterator();
          while(it.hasNext())
          {
               sop(it.next());
          }    

          for (Iterator it1 = al.iterator();it1.hasNext(); )
          {
               sop(it1.next());
          }
     }
}

3,List集合共性方法:

     1,方法:

List:元素是有序的,元素可以重复,应为该集合体系有元素的索引

Set : 元素是无序的,元素不可以重复

boolean add(E e)

          向列表的尾部添加指定的元素(可选操作)。

void add(int index, E element)

          在列表的指定位置插入指定元素(可选操作)。

boolean addAll(Collection<? extends E> c)

          添加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序(可选操作)。

boolean addAll(int index, Collection<? extends E> c)

          将指定 collection 中的所有元素都插入到列表中的指定位置(可选操作)。

void clear()

          从列表中移除所有元素(可选操作)。

boolean contains(Object o)

          如果列表包含指定的元素,则返回 true。

boolean containsAll(Collection<?> c)

          如果列表包含指定 collection 的所有元素,则返回 true。

boolean equals(Object o)

          比较指定的对象与列表是否相等。

E get(int index)

          返回列表中指定位置的元素。

int hashCode()

          返回列表的哈希码值。

int indexOf(Object o)

          返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。

boolean isEmpty()

          如果列表不包含元素,则返回 true。

Iterator<E> iterator()

          返回按适当顺序在列表的元素上进行迭代的迭代器。

int lastIndexOf(Object o)

          返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。

ListIterator<E> listIterator()

          返回此列表元素的列表迭代器(按适当顺序)。

ListIterator<E> listIterator(int index)

          返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。

E remove(int index)

          移除列表中指定位置的元素(可选操作)。

boolean remove(Object o)

          从此列表中移除第一次出现的指定元素(如果存在)(可选操作)。

boolean removeAll(Collection<?> c)

          从列表中移除指定 collection 中包含的其所有元素(可选操作)。

boolean retainAll(Collection<?> c)

          仅在列表中保留指定 collection 中所包含的元素(可选操作)。

E set(int index, E element)

          用指定元素替换列表中指定位置的元素(可选操作)。

int size()

          返回列表中的元素数。

List<E> subList(int fromIndex, int toIndex)

          返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。

Object[] toArray()

          返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)。

<T> T[]

toArray(T[] a)

          返回按适当顺序(从第一个元素到最后一个元素)包含列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。
       2,eg:

import java.util.*;

class ListDemo

{

     public static void sop(Object obj)

     {

          System.out.println(obj);

     }

     public static void method_1()

     {

          ArrayList al =new ArrayList();

          al.add("java01");

          al.add("java02");

          al.add("java03");

          al.add("java04");

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

               sop("al["+x+"]="+al.get(x));

          for(Iterator it = al.iterator();it.hasNext(); )

          {

               sop(it.next());

          }

     }

    

     public static void main(String[] args)

     {

          method_1();

     }

}

4,ListIterator:
 ListIterator是List集合特有的迭代器。是Iterator的接口
由于迭代器在迭代的过程中不能使用集合对象的方法对集合中的元素进行操作。
所以只能使用迭代器的方法操作元素,但是Iterator只能进行判断、删除、取出。如果需要修改,添加等操作,需要ListIterator
ListIterator方法摘要:
     void add(E e)
          将指定的元素插入列表(可选操作)。
     boolean hasNext()
                 以正向遍历列表时,如果列表迭代器有多个元素,则返回 true(换句话说,如果 next 返回一个元素而不是抛出异常,则返回 true)。
     boolean hasPrevious()
                 如果以逆向遍历列表,列表迭代器有多个元素,则返回 true。
     E next()
                 返回列表中的下一个元素。
     int nextIndex()
                 返回对 next 的后续调用所返回元素的索引。
     E previous()
                 返回列表中的前一个元素。
     int previousIndex()
                 返回对 previous 的后续调用所返回元素的索引。
     void remove()
                 从列表中移除由 next 或 previous 返回的最后一个元素(可选操作)。
     void set(E e)
                 用指定元素替换 next 或 previous 返回的最后一个元素(可选操作)。
eg:

import java.util.*;

class ListIteratorDemo

{

     public static void sop(Object obj)

     {

          System.out.println(obj);

     }

     public static void method_1()

     {

          ArrayList al =new ArrayList();

          al.add("java01");

          al.add("java02");

          al.add("java03");

          al.add("java04");

          ListIterator it = al.listIterator();

          sop("hasPrevious: "+it.hasPrevious());//false

          while(it.hasNext())//正序迭代

          {

               Object obj = it.next();

               if(obj.equals("java02"))

//                    al.add("java09");//java.util.ConcurrentModificationException//当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。

//                    it.remove();//将java02的引用从集合中删除。但是Objcet中还有java02

//                    it.add("java09");

                    it.set("java09");

//               sop(obj);

          }

          sop("hasPrevious: "+it.hasPrevious());//true

          sop("hasNext: "+it.hasNext());//false

          while(it.hasPrevious())//倒序迭代

          {

               sop(it.previous());

          }

          sop(al);

     }

     public static void main(String[] args)

     {

          method_1();

     }

}

5,
Vector:
Enumeration<E> elements()
          返回此向量的组件的枚举。
Enumeration:
boolean hasMoreElements()
          测试此枚举是否包含更多的元素。
E nextElement()
          如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。
import java.util.*;
class VectorDemo
{
     public static void sop(Object obj)
     {
          System.out.println(obj);
     }
     public static void method_1()
     {
          Vector al =new Vector();
         
          al.add("java01");
          al.add("java02");
          al.add("java03");
          al.add("java04");
          Enumeration e = al.elements();
          while(e.hasMoreElements())
          {
               sop(e.nextElement());
          }
     }
     public static void main(String[] args)
     {
          method_1();
     }
}
6,LinkedList:
      1,特有方法摘要:

     添加元素:
          addFirst();
          addLast();
     获取元素:但不删除元素,如果集合中没有该元素,会出现NoSuchElementException
          getFirst();
          getlast();
         
     删除元素:获取元素,但元素被删除。如果集合中没有元素,出现NoSuchElemenException
          removeFirst();
          removeLast();


     2,JDK1.6出现了替代方法:

     添加元素:
          offerFirst();
          oferLast();
     获取元素:但不删除元素。如果集合中没有元素,会返回null
          peekFirst();;
          peekLast();
     删除元素:取元素,但元素被删除。如果集合中没有元素,会返回null
          pollFirst();

          pollLast();   

     3,eg:利用LinkList模拟一个堆栈或者队列数据结构

                    import java.util.*;

class DuiLie

{

     private LinkedList link;

     DuiLie()

     {

          link = new LinkedList();

     }

     public void Myadd(Object obj)

     {

          link.add(obj);

     }

     public Object Myget()

     {

          return link.removeLast();

//          return link.removeFirst();

     }

     public boolean isNull()

     {

          return link.isEmpty();

     }

}

class DuiLieDemo

{

     public static void main(String[] args)

     {

          DuiLie d = new DuiLie();

          LinkedList link = new     LinkedList();

          d.Myadd("java01");

          d.Myadd("java02");

          d.Myadd("java03");

          d.Myadd("java04");

          Iterator it = link.iterator();

          while(!(d.isNull()))

          {

               System.out.println(d.Myget());

          }

     }

}

7,ArrayList练习:

         eg1:去除ArrayList集合中的重复元素:
               import java.util.*;

class ArrayListTest1

{

     public static void main(String[] args)

     {

          ArrayList al = new ArrayList();

          al.add("java01");

          al.add("java02");

          al.add("java03");

          al.add("java04");

          al.add("java04");

//          Single(al);

          System.out.println(Single(al));

     }

     public static ArrayList Single(ArrayList al)

     {

          ArrayList Newal = new ArrayList();

          Iterator it = al.iterator();

          while(it.hasNext())

          {

               Object obj = it.next();

               if(!Newal.contains(obj))

                    Newal.add(obj);

          }

          return Newal;

     }

}

         eg2:将自定义对象作为参数存储到集合中,并且去除重复元素:例如同姓名同年龄为同一个人

import java.util.*;

class person

{

     private String name;

     private int age;

     person(String name,int age)

     {

          this.name=name;

          this.age=age;

     }

     public String getName()

     {

          return name;

     }

     public int getAge()

     {

          return age;

     }

     public boolean equals(Object obj)

     {

          if(!(obj instanceof person))

               return false;

          person p = (person)obj;

//          System.out.println(this.name+"......"+p.name);//比较过程

//      System.out.println(this.age+"......."+p.age);

          return this.name.equals(p.name) && this.age == age;//该句中的equals是String中的方法

     }

}

class HashSetDemo

{

     public static void main(String[] args)

     {

          ArrayList al = new ArrayList();//ArrayList判断元素是否相同时调用对象的equals与另一个对象比较,而person中的equals是继承了Objcet的equals:比较的是地址值,因此应该在person中复写equals方法

          al.add(new person("java01",21));//Objcet obj = new person();

          al.add(new person("java02",23));

          al.add(new person("java03",45));

          al.add(new person("java04",78));

          al.add(new person("java04",78));

         

          ArrayList al1 = Single(al);

          Iterator it = al1.iterator();

          while(it.hasNext())

          {

               Object obj = it.next();

               person p = (person)obj;//由于Objcet中没有getName、getAge方法,这里必须进行强转

               sop(p.getName()+":::"+p.getAge());

          }

     }

     public static void sop(Object obj)

     {

          System.out.println(obj);

     }

     public static ArrayList Single(ArrayList al)

     {

          ArrayList newAl = new ArrayList();

          Iterator it = al.iterator();

          while(it.hasNext())

          {

               Object obj = it.next();

               if(!newAl.contains(obj))

                    newAl.add(obj);

          }

          return newAl;

     }

}

7,HashSet


       集合中的元素是无序的(存入和去除的顺序不一定一致),元素不可以重复。
       Set集合的功能和Collection是一致的。
        HashSet是如何保证元素唯一性的呢?
              是通过元素的两个方法,hashCode和equals来完成。
              如果元素的HashCode值相同,才会判断equals是否为true。
              如果元素的hashcode值不同,不会调用equals。
       注意,对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashcode和equals方法。
       eg:
               

import java.util.*;

class HashSetDemo

{

     public static void sop(Object obj)

     {

          System.out.println(obj);

     }

    

     public static void main(String[] args)

     {

          HashSet hs = new HashSet();

          sop(hs.add("java01"));//true

          sop(hs.add("java01"));//false

          hs.add("java02");

          hs.add("java03");

          hs.add("java04");

          Iterator it = hs.iterator();

          while(it.hasNext())

          {

               sop(it.next());

          }

     }

}

8,HashSet存储自定义对象:

     往hashSet集合中存入自定对象,姓名和年龄相同为同一个人,重复元素。
           eg:
                  import java.util.*;

class person

{

     private String name;

     private int age;

     person(String name,int age)

     {

          this.name=name;

          this.age=age;

     }

     public String getName()

     {

          return name;

     }

     public int getAge()

     {

          return age;

     }

     public int hashCode()

     {

//          Syste.out.println(this.name+"...hashCode");

          return this.name.hashCode()+age*39;//返回name的哈希值+age*39,*39是为了避免对象的哈希值重复

     }

     public boolean equals(Object obj)

     {

          if(!(obj instanceof person))

               return false;

          person p = (person)obj;

//          System.out.println(this.name+"......"+p.name);//比较过程

//      System.out.println(this.age+"......."+p.age);

          return this.name.equals(p.name) && this.age == age;//该句中的equals是String中的方法

     }

}

class HashTest

{

     public static void main(String[] args)

     {

          HashSet hs = new HashSet();

          hs.add(new person("java01",21));//Objcet obj = new person();

          hs.add(new person("java02",23));

          hs.add(new person("java03",45));

          hs.add(new person("java04",78));

          hs.add(new person("java04",78));

         

          HashSet al1 = Single(hs);

          Iterator it = al1.iterator();

          while(it.hasNext())

          {

               Object obj = it.next();

               person p = (person)obj;//由于Objcet中没有getName、getAge方法,这里必须进行强转

               sop(p.getName()+":::"+p.getAge());

          }

     }

     public static void sop(Object obj)

     {

          System.out.println(obj);

     }

     public static HashSet Single(HashSet al)

     {

          HashSet newAl = new HashSet();

          Iterator it = al.iterator();

          while(it.hasNext())

          {

               Object obj = it.next();

               if(!newAl.contains(obj))

                    newAl.add(obj);

          }

          return newAl;

     }

}

               
9,HashSet判断和删除的依据
          ArrayList判断元素是否存在,删除元素依据的是equals方法
            HashSet判断元素是否存在,删除元素依据的是hashCode方法和equals方法

【上篇】
【下篇】

抱歉!评论已关闭.