📄 stringcounter.java
字号:
// StringCounter.java
// A convenient class that keep tracks of pairs of
// Strings, and can report some useful statistics on them.
import java.util.Vector;
/**
* A StringCounter object summarizes any number of
* pairs of Strings;
* the first String in each pair is the "attribute" and the second String
* is the "class". When the same pair of Strings is add'd
* more than once to a StringCounter object, the object
* increments an internal count variable. Thus the space
* requirements is big O of the number of distinct pairs
* inserted into the object.
* @author Dan Frost
*/
public class StringCounter
{
// inner class
class Triple
{
String attrValue;
String classValue;
int count;
Triple(String a, String c)
{
attrValue = a;
classValue = c;
count = 1;
}
boolean holds(String a, String c)
{
return attrValue.equals(a) && classValue.equals(c);
}
void increment()
{
++count;
}
}
private Vector<Triple> tripleList = new Vector<Triple>();
private int numRecords = 0;
// These Vectors will have one entry for each unique
// attrValue or classValue in tripleList.
private Vector<String> attrValues = new Vector<String>();
private Vector<String> classValues = new Vector<String>();
/**
* The add method puts a new pair of Strings into
* the StringCounter.
* @param attrValue the first String stored, the "attribute"
* @param classValue the second String stored, the "class"
*/
public void add(String attrValue, String classValue)
{
++numRecords;
for (int i=0; i<tripleList.size(); i++)
{
Triple t = (Triple)(tripleList.elementAt(i));
if (t.holds(attrValue, classValue))
{
t.increment();
return;
}
}
tripleList.add(new Triple(attrValue, classValue));
if (attrValues.indexOf(attrValue) == -1) // not found
attrValues.add(attrValue);
if (classValues.indexOf(classValue) == -1) // not found
classValues.add(classValue);
}
/**
* numDistinctAttrValues returns the number of distinct
* attribute values that have been add'd to this object.
* For instance, if the following calls were made:
* add("red", "cat"), add("blue", "dog"),
* add("red", "cat"), add("green", "cat"),
* add("blue, "cat") then the return value of this
* method would be 3.
* @return the number of unique attribute values in the object.
*/
public int numDistinctAttrValues()
{
return attrValues.size();
}
/**
* numDistinctClassValues returns the number of distinct
* class values that have been add'd to this object.
* For instance, if the following calls were made:
* add("red", "cat"), add("blue", "dog"),
* add("red", "cat"), add("green", "cat"),
* add("blue, "cat") then the return value of this
* method would be 2.
* @return the number of unique class values in the object.
*/
public int numDistinctClassValues()
{
return classValues.size();
}
/**
* getClassValue returns the class value String associated
* with the index parameter.
* For instance, if the following calls were made:
* add("red", "cat"), add("blue", "dog"),
* add("red", "cat"), add("green", "cat"),
* add("blue, "cat") then the return value of getClassValue(1)
* would be "dog".
* @param classIndex index (0 based) into the list of distinct class values.
* @return String associated with the index parameter.
*/
public String getClassValue(int classIndex)
{
return (String)(classValues.elementAt(classIndex));
}
/**
* attrValOccurences returns the number of calls to add
* that were made with the attribute indexed by the
* parameter, regardless of the class String associated
* with the attribute.
* For instance, if the following calls were made:
* add("red", "cat"), add("blue", "dog"),
* add("red", "cat"), add("green", "cat"),
* add("blue, "cat") then the return value of attrValOccurences(1)
* would be 2, since the 1'th attribute is "blue" and it
* was add'ed twice.
* @param attrIndex 0 based index into the list of distinct attribute values.
* @return number of calls to add with that value.
*/
public int attrValOccurences(int attrIndex)
{
if (attrIndex < 0 ||
attrIndex >= attrValues.size())
return 0;
String a = (String)(attrValues.elementAt(attrIndex));
int count = 0;
for (int i=0; i<tripleList.size(); i++)
{
Triple t = (Triple)(tripleList.elementAt(i));
if (a.equals(t.attrValue))
count += t.count;
}
return count;
}
/**
* attrAndClassValOccurences returns the number of calls to add
* that were made with the specific attribute and class values
* indicated by the index parameters.
* For instance, if the following calls were made:
* add("red", "cat"), add("blue", "dog"),
* add("red", "cat"), add("green", "cat"),
* add("blue, "cat") then the return value of attrAndClassOccurences(0, 0)
* would be 2, since the 0'th attribute is "red", the 0'th
* class is "cat", and the two values were add'ed twice.
* @param attrIndex 0 based index into the list of distinct attribute values.
* @param classIndex 0 based index into the list of distinct class values.
* @return number of calls to add with those two values.
*/
public int attrAndClassOccurences(int attrIndex, int classIndex)
{
if (attrIndex < 0 ||
attrIndex >= attrValues.size() ||
classIndex < 0 ||
classIndex >= classValues.size())
return 0;
String a = (String)(attrValues.elementAt(attrIndex));
String c = (String)(classValues.elementAt(classIndex));
for (int i=0; i<tripleList.size(); i++)
{
Triple t = (Triple)(tripleList.elementAt(i));
if (a.equals(t.attrValue) &&
c.equals(t.classValue))
return t.count;
}
// Attribute value a and class value c don't occur together.
return 0;
}
/**
* numAdded returns the number of calls to add
* that have been made on this object.
* For instance, if the following calls were made:
* add("red", "cat"), add("blue", "dog"),
* add("red", "cat"), add("green", "cat"),
* add("blue, "cat") then the return value of numAdded
* would be 5.
* @return number of String pairs add'ed to the object.
*/
public int numAdded()
{
return numRecords;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -