Back to slide -- Keyboard shortcut: 'u'        next  VectorDemo1.java - Et program som demonstrerer brugen af klassen Vector i et Java program.Lecture 5 - slide 9 : 28
Program 1

import java.util.Vector;
import oopcourse.util.Console;

class VectorDemo1 {

 static Vector store = new Vector(5,5);
 
 /** Insert element in v, such that v remains sorted */
 static void insert(String element, Vector v){
  int i = 0;
  while (i < v.size() && 
         ((String)v.elementAt(i)).compareTo(element) < 0
        )
    i = i + 1;
 
  v.insertElementAt(element,i);
 }
 
 
 public static void main(String[] args){
   String str;
   do{
     str = Console.readString("Type a string to be inserted: " +
                              "(empty string terminates): ");
     if (str != ""){
       insert(str,store);
       System.out.println(store);
      }
     } while (str != "");
 }
}