vectortest.java

来自「java网络高级编程的配套源码,java网络高级编程为清华出版社出版.」· Java 代码 · 共 68 行

JAVA
68
字号
/*源程序清单6-3*/

package test;

import java.util.*;
import java.awt.*;

public class VectorTest
{
  Vector sam=new Vector(15,3);

  public VectorTest()
  {
    System.out.println("The vector's init capacity is:"+sam.capacity());
    System.out.println("The vector's init size is:"+sam.size());
    System.out.println("Insert 0-9 to Vector");
    for(int i=0;i<10;i++)
      sam.addElement(new Integer(i));
    System.out.println("The vector's capacity is:"+sam.capacity());
    System.out.println("The vector's size is:"+sam.size());
    System.out.println("Insert button0-button9 to Vector");
    for(int i=0;i<10;i++)
    {
      Button button=new Button("button"+Integer.toString(i));
      sam.addElement(button);
    }
    System.out.println("The vector's capacity is:"+sam.capacity());
    System.out.println("The vector's size is:"+sam.size());
    int index=sam.indexOf(new Integer(6));
    System.out.println("The index of 6 in the vector is:"+index);
    int firstNumber=((Integer)sam.firstElement()).intValue();
    System.out.println("The first element in the vector is:"+firstNumber);
    Button button=(Button)sam.lastElement();
    System.out.println("The last element in the vetor is:"+button.getLabel());
    System.out.println("Remove 4 element in the vector");
    for(int i=8;i<12;i++)
      sam.removeElementAt(i);
    System.out.println("The vector's capacity is:"+sam.capacity());
    System.out.println("The vector's size is:"+sam.size());
    System.out.println("Trim the vector to size");
    sam.trimToSize();
    System.out.println("The vector's capacity is:"+sam.capacity());
    System.out.println("The vector's size is:"+sam.size());
    System.out.println("Remove all element of the vector");
    sam.removeAllElements();
    System.out.println("The vector's capacity is:"+sam.capacity());
    System.out.println("The vector's size is:"+sam.size());
    sam.trimToSize();
    System.out.println("Trim the vector to size");
    System.out.println("The vector's last capacity is:"+sam.capacity());
    System.out.println("The vector's last size is:"+sam.size());
  }

  public static void main(String[] args)
  {
    try
    {
      new VectorTest();
      System.in.read();
    }
    catch(Exception e)
    {
      System.out.println(e.toString());
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?