当前位置: 技术问答>java相关
SL275例题
来源: 互联网 发布时间:2015-07-31
本文导语: SL275 教材Module 9中关于Map 的Example如下: import java.util.Map; import java.util.HashMap; import java.util.Iterator; import java.io.FileReader; class MapExample { public static void main(String[] args) throws java.io.FileNotFoundException { Map word_oc...
SL275 教材Module 9中关于Map 的Example如下:
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.FileReader;
class MapExample
{
public static void main(String[] args) throws java.io.FileNotFoundException
{
Map word_ocunt_map=new HashMap();
FileReader reader=new FileReader(args[0]);
Iterator words=new WordStreamIterator(reader);
while(words.hasNex())
{
String word=(String)words.next();
String word_lowercase=word.toLowerCase();
Integer frequency=(Integer)word_count_map.get(word_lowercase);
if(frequency==null)
{
frequency=new Integer(1);
}
else
{
int value=frequency.intValue();
frequency=new Integer(value+1);
}
word_count_map.put(word_lowercase,frequency);
}
System.out.println(word_count_map);
}
}
可是编译时不认识WordStreamIterator类哦,为什么?例题错了吗?
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.FileReader;
class MapExample
{
public static void main(String[] args) throws java.io.FileNotFoundException
{
Map word_ocunt_map=new HashMap();
FileReader reader=new FileReader(args[0]);
Iterator words=new WordStreamIterator(reader);
while(words.hasNex())
{
String word=(String)words.next();
String word_lowercase=word.toLowerCase();
Integer frequency=(Integer)word_count_map.get(word_lowercase);
if(frequency==null)
{
frequency=new Integer(1);
}
else
{
int value=frequency.intValue();
frequency=new Integer(value+1);
}
word_count_map.put(word_lowercase,frequency);
}
System.out.println(word_count_map);
}
}
可是编译时不认识WordStreamIterator类哦,为什么?例题错了吗?
|
WordStreamIterator不在java.lang下,这样一定出错。他好像连jdk里都没有。可能是自己做的类。
|
JBUILDER有这样的功能,我用的是JB7,如果引入了一个当前不可见的类,JB会有提示,点提示以后JB会去搜索所有的J2SE API和当前classpath里所有的包,如果找到以后它会自己帮你写入程序里 import xxx.xxx.xxx;这样的。
WordStreamIterator不是在标准的J2SE API里,应该是教材自己提供的。
WordStreamIterator不是在标准的J2SE API里,应该是教材自己提供的。
|
JBUILDER里有find definition的功能可以找定义。
java的类是比较规矩的。按照惯例,WordStreamIterator这样的名字,应该是Iterator的subclass或subinterface。查查Iterator文档,看看有没有这个东西,一般就知道了。
java的类是比较规矩的。按照惯例,WordStreamIterator这样的名字,应该是Iterator的subclass或subinterface。查查Iterator文档,看看有没有这个东西,一般就知道了。
|
关注
|
WordStreamIterator是sl-275定义的,在labfilemod09examples有的,如下:
import java.util.Iterator;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
/**
* This class presents an iterator over words in an input stream.
* A "word" is defined as any contiguous sequence of letters (including
* an apostrope for possesive words). All whitespace and punctuation
* is removed.
*/
public class WordStreamIterator implements Iterator {
private static char APOSTROPHE = ''';
private BufferedReader input;
private String current_line;
private int current_index;
private String next_word = null;
/**
* This is the basic constructor.
*/
public WordStreamIterator(InputStreamReader stream) {
input = new BufferedReader(stream);
// Prime the word pump
nextLine();
nextWord();
}
/**
* This method determines whether or not there are any more words.
*/
public boolean hasNext() {
return (next_word != null);
}
/**
* This method return the "next word" in the stream.
*
* The "next word" is already available (an may be null), but this
* method must move forward in the stream to get the next, next word.
*/
public Object next() {
String result = next_word;
nextWord();
return result;
}
/**
* The remove method is not supported.
*/
public void remove() {
throw new UnsupportedOperationException();
}
/**
* This private method is used to read the next line in the
* input stream.
*
* current_line will be null when we reach the EOF.
*/
private void nextLine() {
try {
current_line = input.readLine();
if ( current_line == null ) {
input.close();
}
current_index = 0;
} catch (IOException e) {
current_line = null;
}
}
/**
* This private method is used to parse the next word in the
* input stream.
*
* This is the work-horse of this class. It moves the current_index
* through the current_line until it finds the start of a word, it
* then parses the word (storing the letters in a buffer) until it
* reaches the end of the word (a non-letter character or EOL).
*
* next_word will be null when we reach the EOF.
*/
private void nextWord() {
final StringBuffer buffer = new StringBuffer();
char ch;
buffer.setLength(0);
// parse over all non-letters, handling line breaks and EOF
do {
if ( current_line == null ) {
// quit loop on EOF
break;
}
if ( current_index >= current_line.length() ) {
// if at the end of the current line,
// read a new one and continue from the top (could be EOF)
nextLine();
continue;
}
if ( ! Character.isLetter(current_line.charAt(current_index)) ) {
// skip over all non-letters
current_index++;
} else {
// quit loop, when a letter is reached
break;
}
} while ( true );
// parse letters into the buffer
do {
if ( current_line == null ) {
// quit loop on EOF
break;
}
// get the next character
ch = current_line.charAt(current_index);
if ( ! Character.isLetter(ch) && (ch != APOSTROPHE) ) {
// quit loop, when a non-letter is reached
break;
}
// store the character and move to the index forward
buffer.append(ch);
current_index++;
// if we have reached the end of line,
// then get the next line
// and quit the loop (EOL always means the end of a word)
if ( current_index >= current_line.length() ) {
nextLine();
break;
}
} while ( true );
// create the next word, if any
if ( buffer.length() == 0 ) {
next_word = null;
} else {
next_word = buffer.toString();
}
}
}
import java.util.Iterator;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
/**
* This class presents an iterator over words in an input stream.
* A "word" is defined as any contiguous sequence of letters (including
* an apostrope for possesive words). All whitespace and punctuation
* is removed.
*/
public class WordStreamIterator implements Iterator {
private static char APOSTROPHE = ''';
private BufferedReader input;
private String current_line;
private int current_index;
private String next_word = null;
/**
* This is the basic constructor.
*/
public WordStreamIterator(InputStreamReader stream) {
input = new BufferedReader(stream);
// Prime the word pump
nextLine();
nextWord();
}
/**
* This method determines whether or not there are any more words.
*/
public boolean hasNext() {
return (next_word != null);
}
/**
* This method return the "next word" in the stream.
*
* The "next word" is already available (an may be null), but this
* method must move forward in the stream to get the next, next word.
*/
public Object next() {
String result = next_word;
nextWord();
return result;
}
/**
* The remove method is not supported.
*/
public void remove() {
throw new UnsupportedOperationException();
}
/**
* This private method is used to read the next line in the
* input stream.
*
* current_line will be null when we reach the EOF.
*/
private void nextLine() {
try {
current_line = input.readLine();
if ( current_line == null ) {
input.close();
}
current_index = 0;
} catch (IOException e) {
current_line = null;
}
}
/**
* This private method is used to parse the next word in the
* input stream.
*
* This is the work-horse of this class. It moves the current_index
* through the current_line until it finds the start of a word, it
* then parses the word (storing the letters in a buffer) until it
* reaches the end of the word (a non-letter character or EOL).
*
* next_word will be null when we reach the EOF.
*/
private void nextWord() {
final StringBuffer buffer = new StringBuffer();
char ch;
buffer.setLength(0);
// parse over all non-letters, handling line breaks and EOF
do {
if ( current_line == null ) {
// quit loop on EOF
break;
}
if ( current_index >= current_line.length() ) {
// if at the end of the current line,
// read a new one and continue from the top (could be EOF)
nextLine();
continue;
}
if ( ! Character.isLetter(current_line.charAt(current_index)) ) {
// skip over all non-letters
current_index++;
} else {
// quit loop, when a letter is reached
break;
}
} while ( true );
// parse letters into the buffer
do {
if ( current_line == null ) {
// quit loop on EOF
break;
}
// get the next character
ch = current_line.charAt(current_index);
if ( ! Character.isLetter(ch) && (ch != APOSTROPHE) ) {
// quit loop, when a non-letter is reached
break;
}
// store the character and move to the index forward
buffer.append(ch);
current_index++;
// if we have reached the end of line,
// then get the next line
// and quit the loop (EOL always means the end of a word)
if ( current_index >= current_line.length() ) {
nextLine();
break;
}
} while ( true );
// create the next word, if any
if ( buffer.length() == 0 ) {
next_word = null;
} else {
next_word = buffer.toString();
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。