⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vectortest.java

📁 《Java面向对象程序设计》例子源代码.轻松学习书本.
💻 JAVA
字号:
//VectorTest.java
import java.util.*;
public class VectorTest {
   private static final String colors[] = { "red", "white", "blue" };
   public VectorTest(){
      Vector vector = new Vector();
      printVector( vector ); 
      vector.add( "magenta" ); 
      for ( int count = 0; count < colors.length; count++ )
         vector.add( colors[ count ] );         
      vector.add( "cyan" );
      printVector( vector ); 
      try {
         System.out.println( "First element: " + vector.firstElement() );
         System.out.println( "Last element: " + vector.lastElement() );
      }
      catch ( NoSuchElementException exception ) {
         exception.printStackTrace();
      }
      if ( vector.contains( "red" ) )
         System.out.println( "\"red\" found at index " + 
            vector.indexOf( "red" ) );
      else
         System.out.println( "\"red\" not found\n" );      
      vector.remove( "red" ); 
      System.out.println( "\"red\" has been removed" );
      printVector( vector ); 
      if ( vector.contains( "red" ) )
         System.out.println( "\"red\" found at index " + 
            vector.indexOf( "red" ) );
      else
         System.out.println( "\"red\" not found" );
      System.out.println( "Size: " + vector.size() + 
         "\nCapacity: " + vector.capacity() );
   }   
   private void printVector( Vector vectorToOutput ){
      if ( vectorToOutput.isEmpty() ) 
         System.out.print( "vector is empty" );      
      else { 
         System.out.print( "vector contains: " );      
         Enumeration items = vectorToOutput.elements(); 
         while ( items.hasMoreElements() )
            System.out.print( items.nextElement() + " " );
      }      
      System.out.print( "\n" ); 
   }
   public static void main( String args[] ){
      new VectorTest(); 
   } 
} 

⌨️ 快捷键说明

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