Tuning an application
From: Kris M (mRaEcMcOxVoApMhE_at_yahoo.com)
Date: 03/26/05
- Next message: Max010: "java.lang.NullPointerException"
- Previous message: David Alex Lamb: "Re: comp.lang.java.{help,programmer} - what they're for (mini-FAQ 2004-10-08)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 26 Mar 2005 17:28:35 -0500
any suggestions on how to improve this exercise in using vector?
//----------------------------------
//3-25-05
//Accessing and processing data from a vector
//
import java.io.*;
import java.util.*;
class P4
{
static Scanner input = new Scanner(System.in);
static PrintWriter screen = new PrintWriter(System.out, true);
static final int VECTORSIZE = 8;
static final int ROUNDS = 3;
static final int GAMES = 7;
public static void main(String [] args)
{
Vector<String> dataStore = new Vector<String>(VECTORSIZE);
dataStore = filledVector();
screen.println("Initial DataStore Contents");
displayVectorContents(dataStore);
screen.println("__________________________");
for(int round = 0; round < ROUNDS; round++)
{
screen.println("Round =" + (round +1));
dataStore = playRound(dataStore, round);
displayVectorContents(dataStore);
}
screen.println("Champion!");
}
private static Vector playRound(Vector v, int round)
{
Vector<String> rVector = new Vector<String>((v.capacity())/2);
for(int game = 0; game < rVector.capacity(); game++)
{
rVector.addElement(playGame((String)v.firstElement(),(String)v.lastElement()));
v.remove(v.firstElement());
v.remove(v.lastElement());
}
return rVector;
}
private static String playGame(String string1, String string2)
{
int compare = string1.compareTo(string2);
if(compare < 0)
return string1;
else
return string2;
}
private static Vector filledVector()
{
String [] names = {"Team 1" , "Team 2" , "Team 3" , "Team 4" , "Team 5" ,
"Team 6" , "Team 7" , "Team 8"};
Vector<String> v = new Vector<String>(VECTORSIZE);
for (int index =0; index < VECTORSIZE; index++)
v.addElement(names [index]);
return v;
}
private static void displayVectorContents(Vector v)
{
if(!v.isEmpty())
{
for (Enumeration e = v.elements() ; e.hasMoreElements() ;)
{
System.out.println(e.nextElement());
}
}
else
{
screen.println("Vector is empty");
}
}
}
- Next message: Max010: "java.lang.NullPointerException"
- Previous message: David Alex Lamb: "Re: comp.lang.java.{help,programmer} - what they're for (mini-FAQ 2004-10-08)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|