vectortest.java

来自「本源码演示Vector的使用方法」· Java 代码 · 共 49 行

JAVA
49
字号
     import java.util.*;

     class VectorTest extends Object
     {
          public static Vector getRandomScores()
          {
               // create a random number generator
               Random rand = new Random();

               // generate between 500 and 1000 random scores
               int numElements = 500 + Math.abs(rand.nextInt())%501;

               // create a new Vector and fill it in with a bunch of random scores
               Vector v = new Vector(numElements);
               while(numElements > 0)
               {
                    // add an Integer between 0 and 2000
                    v.add(new Integer(Math.abs(rand.nextInt())%2001));
                    numElements--;
               }

               return v;
          }

          // finds the greatest score within a vector of random scores
          public static void main(String[] args)
          {
               int highestScore = 0;    // the highest score we've seen sofar

               // generate some random scores
               Vector scores = getRandomScores();               
               
               // cycle through the enumeration of the scores and pick out
               // the highest one
               for(Enumeration e = scores.elements(); e.hasMoreElements(); )
               {
                    Integer score = (Integer)(e.nextElement());

                    if(score.intValue() > highestScore)
                    {
                         highestScore = score.intValue();
                    }
               }   

               // print out the highest score
               System.out.println(highestScore);
          }
     }    // VectorTest

⌨️ 快捷键说明

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