关闭

Vector Enumeration 示例

标签: vectorintegeriteratorimportstringfloat
876人阅读 评论(0) 收藏 举报
分类:

//Demonstrate various vector operations.
import java.util.*;
class VectorDemo{
 public static void main(String[] args)
 {
  //initial size is 3,increment is 2
  Vector v = new Vector(3,2);

  System.out.println("Initial size :" + v.size());
  System.out.println("Initial Capacity :" + v.capacity());

  v.addElement(new Integer(1));
  v.addElement(new Integer(2));
  v.addElement(new Integer(3));
  v.addElement(new Integer(4));
  System.out.println("Capacity after four additions:" + v.capacity());
  
  v.addElement(new Double(9.99));
  System.out.println("Current capacity :" + v.capacity());

  v.addElement(new Double(77.87));
  v.addElement(new Integer(9));
  System.out.println("Current capacity :" + v.capacity());

  v.addElement(new Float(77.7));
  v.addElement(new Integer(10));
  System.out.println("Current capacity :" + v.capacity());

  v.addElement(new Integer(11));
  v.addElement(new Integer(12));
  System.out.println("First element :" + (Integer)v.firstElement());
  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 enum = v.elements();

  System.out.println("Elements in vector:");
  while(enum.hasMoreElements()){
   System.out.print(enum.nextElement() + " ");
  }
  System.out.println();

  /*
  //use an iterator to display contents
  Iterator i = v.iterator();

  System.out.println("Elements in vector:");
  while(i.hasNext()){
   System.out.print(i.next() + " ");
  }
  System.out.println();
  */
 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:680786次
    • 积分:7852
    • 等级:
    • 排名:第2626名
    • 原创:75篇
    • 转载:247篇
    • 译文:0篇
    • 评论:64条
    文章分类
    最新评论