first commit
This commit is contained in:
100
CS1322/p3/TricksandTools.txt
Normal file
100
CS1322/p3/TricksandTools.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
This file contains a walkthrough on how to use important tools in Java. These
|
||||
include Vectors, Enumerations and StringTokenizers. All of these classes are
|
||||
located in the package java.util
|
||||
|
||||
VECTORS
|
||||
===============================================================================
|
||||
In order to use the Vector class, you need to import java.util.Vector.
|
||||
Look in the Java API to see a list of all the methods that can be used with
|
||||
Vectors. The great thing about Vectors is that they are dynamic like
|
||||
Linked Lists, but they give instantaneous access to elements like arrays.
|
||||
|
||||
So lets say we had a small java class that utilized Vectors. We want to add
|
||||
words when a person says them into the Vector - as long as the word is not in
|
||||
the Vector already. When they are done speaking, we want to print out all of
|
||||
the words that they said.
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
public class SaveWords
|
||||
{
|
||||
public SaveWords(){
|
||||
Vector v = new Vector();
|
||||
String word = "";
|
||||
while((word = WordISay()) != null) // as long as I say a word
|
||||
{
|
||||
if(!(v.contains(word))) // check to see if in Vector
|
||||
{
|
||||
// if not add it
|
||||
v.add(word)
|
||||
}
|
||||
} // end while
|
||||
|
||||
// loop through the vector (notice the size() method)
|
||||
for(int i = 0; i<v.size(); i++)
|
||||
{
|
||||
// use v.elementAt to get a specific element without
|
||||
// removing it.
|
||||
System.out.println(v.elementAt(i));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
This is a simple walkthrough. Other useful and important methods deal with
|
||||
removing an object and getting the index of an object. Look in the API for
|
||||
these methods. Check out the elements method as well. This method returns
|
||||
an Enumeration of all the elements in the Vector (see below).
|
||||
|
||||
|
||||
ENUMERATION
|
||||
============================================================================
|
||||
|
||||
The important methods for Enumeration are
|
||||
hasMoreElements and nextElement.
|
||||
|
||||
hasMoreElements returns true if there are more elements while nextElement will
|
||||
return the next Object of the Enumeration. Example of use...
|
||||
|
||||
Enumeration e.....
|
||||
|
||||
while(e.hasMoreElements()){
|
||||
|
||||
// do something with e.nextElement()
|
||||
// remember that this is an Object and may need to be casted into the
|
||||
// appropriate Object
|
||||
|
||||
} // end while
|
||||
|
||||
|
||||
STRING TOKENIZER
|
||||
=============================================================================
|
||||
|
||||
The StringTokenizer class is very similar to the Enumeration class with respect
|
||||
to the methods that are able to be called. The small difference is that the
|
||||
actual methods are called hasMoreTokens() and nextToken(). The nextToken method
|
||||
returns a String as opposed to an Object. The StringTokenizer class does
|
||||
exactly what it says...it tokenizes (breaks up) a string. The default delimiter
|
||||
is white space. Thus if a StringTokenizer was declared as
|
||||
StringTokenizer st = new StringTokenizer("How Are you doing?");
|
||||
|
||||
then the code
|
||||
while(st.hasMoreTokens()){
|
||||
System.out.println(st.nextToken());
|
||||
}
|
||||
|
||||
would produce the following output
|
||||
How
|
||||
Are
|
||||
you
|
||||
doing?
|
||||
|
||||
if instead the StringTokenizer was declared to delimit with respect to o's
|
||||
StringTokenizer st = new StringTokenizer("How Are you doing?", "o");
|
||||
|
||||
then the output would be...
|
||||
H
|
||||
w Are y
|
||||
u d
|
||||
ing?
|
||||
Reference in New Issue
Block a user