几个接口和类(易忘)

枚举(Enumeration)

作用:它是一个接口, 遍历集合

方法:boolean hasMoreElements()   Object nextElement()

代码:

import java.util.Vector;

import java.util.Enumeration;//和集合相关的类, 接口都放在java.util包中

class EnumerationDemo{

public static void main(String[] args){

//声明了一个Enumeration接口类型的引用

Enumeration days;

//Vector, 类

//Vector实例化时, 会在内存开辟一个能包含10个元素的数组

Vector dayNames = new Vector();

//boolean add(Object obj). 该方法也使用了泛型

dayName.add("Sunady");

dayName.add("Monday")

dayName.add("Tuesday");

dayName.add("Wednesady");

dayName.add("Thursday");

dayName.add("Friday");

dayName.add("Saturady");

//Enumeration elements(). 执行这个方法返回枚举列表

days = dayNames.elements();

//boolean hasMoreElements(). 检查枚举列表中是否存在枚举值

while(days.hasMoreElements()){

//boolean nextElement(). 调用Enumeration类中的nextElement()取枚举值

System.out.printl(days.nextElement());

}

}

}

向量(Vector)

描述:  底层结构是一个可随需求改变容器大小的动态数组. Vector容器默认存放10个元素, 当容器容量不足时, 自动扩充容器.新的容器是原来的1.5倍

Vector类和ArrayList类的区别:

1)线程问题. Vector类中的好多方法实现了同步, 所以Vector类是支持同步的

Vector类的4种构造方法

1)Vector() 该方法在Vector实例化时自动生成一个能存放10个元素的集合空间

2)Vector(int size) 该方法在Vector实例化的时候人为指定集合空间

3)Vector(int size, int incor) 该方法在Vector实例化时人为指定了空间的初始大小和容器的增量(每次增加的容器大小)

4)Vector(Collecton c) Vector实例化时以集合c中的元素初始化

Vector类中的常用方法:

1)void add(int index,Object element) 在Vector集合的指定位置添加element

2)boolean add(Object o) 将指定元素添加到Vector集合的末尾

3)boolean addAll(Collection c) 将集合c中的所有元素添加到当前集合中

4)void addElement(Object obj) 向当前集合中添加元素obj, 并增加容器容量 

5)int capacity() 计算容器的容量

6)void clear() 清楚集合中所有的元素

7)Object clone() 制造集合副本

8)boolean contains(Object obj) 判断集合中是否包含指定的元素. 若包含, 返回true

9)boolean containsAll(Collection c) 判断当前集合是否包含指定集中的所有元素. 若包含, 返回true

10)void copyInto(Object[] arr) 将当前集合中元素复制到指定数组中

11)Object elementAt(int index) 取出集合中指定索引处的元素

12)Enumeration elements() 返回集合对应的枚举列表

13)void ensureCapacity(int minCapacity) 增加集合容量, 以确保能放下所有元素

14)boolean equals(Object obj) 比较指定对象与向量的相等性

15)Object firstElement()返回集合中的第一个元素

16)Object get(int index) 根据索引取值

17)int hashCode() 取当前向量的哈希值

18)int indexOf(Object obj) 判断当前集合是否包含指定的元素. 若包含, 返回该元素第一次出现时对应的索引, 反之, 返回-1

19)int indexOf(Object obj,int index) 从index索引处遍历Vector集合. 若集合中存在obj对象, 返回该元素对应的索引 

20)void insertElementAt(Object obj,int index) 在Vector集合中index索引处添加元素obj

21)boolean isEmpty() 判断当前集合是否为空

22)Object lastElement() 返回当前集合的最后一个元素

23)int lastIndexOf(Object obj) 遍历集合. 如果集合中存在obj, 返回该元素最后一次出现时的索引

24)int lastIndexOf(Object obj,int index) 从集合的index处逆序遍历数组. 若集合中存在该元素存在该obj, 返回该元素最后一次出现时的索引

25) boolean remove(int index) 根据索引删除集合中的元素

26)boolean remove(Object obj) 根据元素删除集合中的元素. 即遍历集合, 判断集合中是否存在该元素, 如果存在, 删除第一次出现时的元素 
27)boolean removeAll(Collection c) 将Vectoe集合中与Collection集合中的元素相同的元素

28)void removeAllElements() 删除当前集合中的所有元素

29)boolean removeElement(Object obj) 判断当前集合是否存在obj. 若存在,删除第一次出现时的元素

30)void removeElement(int index) 根据索引删除元素

31)protected void removeRange(int fromIndex,int toIndex) 删除集合中索引fromIndex~toIndex之间的元素

32)boolean retainAll(Collection c) 取当前Vector集合和Collection集合的交集, 保留在当前集合中

33)Object set(int index,Object obj) / void setElementAt(Object obj,int index)在当前集合的指定索引处插入指定元素, 返回被替换的元素

34)void setSize(int newSize) 设置当前集合的容量

35)int size() 返回当前集合的元素个数 

36)List subList(int fromIndex,int toIndex) 返回Vector集合的部分视图. Vector实现List接口

36)Object[] toArray() 当前集合转数组

37)String toString() 当前集合以字符串形式输出 

38)void trimToSize() 调整当前集合的容量大小,使其成为满向量

代码:

import java.util.*;

class VectorDemo {

public static void main(String args[]) {

      // initial size is 3, increment is 2
      Vector v = new Vector(3, 2);//容量3 增量2
      System.out.println("Initial size: " + v.size());//容器中元素个数
      System.out.println("Initial capacity: " +
      v.capacity());
      v.add(new Integer(1));
      v.add(new Integer(2));
      v.add(new Integer(3));
      v.add(new Integer(4));
      System.out.println("Capacity after four additions: " +
          v.capacity());

      v.add(new Double(5.45));
      System.out.println("Current capacity: " +
      v.capacity());
      v.add(new Double(6.08));
      v.add(new Integer(7));
      System.out.println("Current capacity: " +
      v.capacity());
      v.add(new Float(9.4));
      v.add(new Integer(10));
      System.out.println("Current capacity: " +
      v.capacity());
      v.add(new Integer(11));
      v.add(new Integer(12));
      System.out.println("First element: " +
         (Integer)v.firstElement());//函数默认返回Object类型数据
      System.out.println("Last element: " +
         (Integer)v.lastElement());
      if(v.contains(new Integer(3)))
         System.out.println("Vector contains 3.");
      // enumerate the elements in the vector.
      Enumeration vEnum = v.elements();
      System.out.println("\nElements in vector:");
      while(vEnum.hasMoreElements())
         System.out.print(vEnum.nextElement() + " ");
      System.out.println();
   }
}

栈(Stack)

描述:栈, Vector的子类. 标准的先进后出. 栈有一些自己独立的方法

方法:

1)boolean empty() 判断栈是否为空

2)Object peek() 查看栈顶元素

3)Object pop() 移除栈顶元素

4)Object push(Object obj) 将指定元素压入栈中

5)int search(Object obj) 查看指定元素在栈中的索引

代码:

import java.util.*;

class StackDemo{

public static void showpush(Stack st,int a){

//把基本整型封装成包装类

//整型的包装类对象压入栈中

st.push(new Integer(a));

System.out.println("push(" + a +")");

System.out.println("stack:" + st);

}

public static void showpop(Stack st){

System.out.println("pop --> ");

//取出栈顶元素. 

Integer a = (Integer)st.pop();

System.out.println(a);

System.out.println("Stack:" + st);

}

public static void main(String[] args){

//创建空栈. 这里,构造方法没有提初始大小

Stack st = new Stack();

//栈是如何输出的呢?

//Stack继承于Vector, 栈的输出调用了Vector中的toString()

System.out.println("stack:" + st);

showpush(st,42);

showpush(st,66);

showpush(st,99);

showpop(st);

showpop(st);

showpop(st);

try{

showpop(st);

}catch(EmptyStackException e){

System.out.println("empty stack");

}

}

}

Map集合

描述:Map, 接口

Map集合中存放的是键值对. Map集合中不为空时, 我们可以通过键访问值

异常:

1)当访问的值不存在的时候,方法就会抛出一个NosuchElementException异常

2)当对象的类型和Map里的元素类型不一致的时候,抛出 ClassCastException异常(用到泛型的时候。。。)

3)当在不允许使用null对象的Map集合中使用null对象,会抛出一个NullPointerException异常

4)当尝试一个只读的Map时, 会抛出一个UnsupportedOperationException异常

方法:

1)void clear() 删除Map集合中所有的键值对

2)boolean containsKey(Object k) 判断Map集合中是否包含指定的键. 若包含, 返回true

3)boolean containsValue(Object v) 判断Map集合中是否包含指定的值. 若包含, 返回值

4)Set entrySet() 将Map集合中的键值对视图封装在Set集合中,并返回

5)boolean equals(Object obj) 指定的对象和Map集合中的键作比较. 若相等,返回true. 由此,保证集合中键的唯一性

6)Object get(Object k) 根据键得到Map集合中对应的值. 如果指定的键在集合中不存在, 返回null

7)int hashCode() 计算Map集合中键值对的哈希值. 这里注意:函数返回值是整型

8)boolean isEmpty() 判断Map集合是否为空. 若为空,返回true

9)Set keySet() 取Map集合中的键封装到一个Set集合中, 并返回

10)Object put(Object k,Object v) 向Map集合中添加键值对, 并返回值. API上是这样写的: V put(K k,V v)

11)Object remove(Object k) 判断Map集合中是否包含指定的键. 若包含, 删除对应键值对, 并返回值

12)int size() 判断Map集合中键值对的个数

13)Collection values() 取Map集合中的值封装在Collection集合中

代码:

import java.util.*;


class MapDemo {


     public static void main(String[] args) {
      Map m1 = new HashMap(); 
      m1.put("Zara", "8");
      m1.put("Mahnaz", "31");
      m1.put("Ayan", "12");
      m1.put("Daisy", "14");
      System.out.println();
      System.out.println(" Map Elements");
      System.out.print("\t" + m1);
   }
}




























  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值