被忽略的java.io.StreamTokenizer

本文介绍Java中的StreamTokenizer类,详细讲解其使用方法及常见配置,如设置注释字符、普通字符与单词字符等,并通过实例演示如何进行字符串分割。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一直以来,偶们都知道字符串的分割最常用的是java.util.StringTokenizer,但是某些时候,StringTokenizer有很大的局限性,孰不知,有个更为强大的的分割工具,不过是基于流的,但是借助于中间类,也可以处理字符串,而且处理的很好,今天偶们就掀开他地盖头来瞧一瞧:
   StreamTokenizer定义了几种基本的常量用于标识解析过程:TT_EOF(流结尾)、TT_EOL(行结尾)、TT_NUMBER(数字符号, 0 1 2 3 4 5 6 7 8 9 . -都属于数字语法)、TT_WORD(一个单词)。
   其含有的基本方法介绍一下:
   commenChar(int ch) - 指定某个字符为注释字符,此字符之后直到行结尾都被stream tokenizer忽略。
   eolIsSignificant(boolean flag) - 决定一个行结束符是否被当作一个基本的符号处理,如果是true,则被当作一个基本符号,不当作普通的分隔符,如果是false,则保持原义,即当作普通的分隔符。
   lineno() - 返回当前流所在的行号。
   lowerCaseMode(boolean flag) - 决定是否读取一个单词时是否转变成小写。
   nextToken() - 不用解释了吧。。。
   ordinaryChar(int ch) - 指定字符在这个tokenizer中保持原义,即只会把当前字符认为普通的字符,不会有其他的语义。
   ordinaryChars(int low, int hi) - 指定范围内的字符保持语义,同上
   parseNumbers() - 当stream tokenizer遭遇到一个单词为双精度的浮点数时,会把它当作一个数字,而不是一个单词。
   pushBack() - 回退,会引起下一个nextToken方法返回当前值。
   quoteChar(int ch) - 指定当前字符为当前tokenizer中的分隔符,在两个符号之间被当作一个字符串解析。
   resetSyntax() - 重置语法表使所有的字符都被认为是“ordinary”。
   slashSlashComments(boolean flag) - 如果为true,则/*与*/之间的都被认为是注释,反之,不是。
   slashStartComments(boolean flag) - 如果为true,则//之后到行结尾的所有都被认为是注释,反之,不是。 
   whitespaceChars(int low, int hi) - 字符low与hi之间的所有字符都被当作为空格符,即被认识为tokenzier的分隔符。
   wordChars(int low, int hi) - 字符low与hi之间的所有字符都被当作为单词的要素。一个单词是由一个单词要素后面跟着0个或者更多个单词要素或者数字要素。
   api不是很复杂吧~应该不难理解。
   一下举个例子会一幕了然: // Set up a StreamTokenizer on the characters in this String
   StreamTokenizer  st  = new  StreamTokenizer (new  StringReader (svalue ));
  //知道该怎么解析字符串了吧。
   st.whitespaceChars (',',',');
   // Commas are delimiters
   st.ordinaryChars ('0''9');
   // Needed to turn off numeric flag
   st.ordinaryChars ('.''.');
   st.ordinaryChars ('-''-');
   st.wordChars ('0''9');
   // Needed to make part of tokens
   st.wordChars ('.''.');
   st.wordChars ('-''-');
   // Split comma-delimited tokens into a List
   ArrayList  list  = new  ArrayList ();
   while  (true ) 
  {
       int  ttype  = st.nextToken ();
       if  ((ttype  == StreamTokenizer.TT_WORD ) || (ttype  > 0 )) 
      {
           list.add (st.sval );
           
  
    } else  if  (ttype  == StreamTokenizer.TT_EOF ) 
      {
           break ;
           
  
    } else  
      {
           throw  new  ConversionException  ("Encountered token of type " + ttype );
           
  
    } 
  
}
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top */ import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, sc, out); out.close(); } static class Task { public boolean aStar(int begin,int end,ArrayList<int[]>[] G,long[] f,int k,int n,int lim) { PriorityQueue<long[]> que=new PriorityQueue<long[]>(new Comparator<long[]>() { @Override public int compare(long[] o1, long[] o2) { if(o1[1]>o2[1]) return 1; if(o1[1]<o2[1]) return -1; return 0; } }); if(f[begin]==Integer.MAX_VALUE) return false; int[] cnt=new int[n+1]; que.offer(new long[] {begin,f[begin],0}); while(!que.isEmpty()) { long[] temp=que.poll(); int u=(int) temp[0]; cnt[u]+=1; if(cnt[u]>k) continue; if(cnt[u]==k&&u==end) { return temp[1]<=lim; } for(int[] edge:G[u]) { if(cnt[edge[0]]<k) { que.offer(new long[] {edge[0],temp[2]+f[edge[0]]+edge[1],temp[2]+edge[1]}); } } } return false; } public void solve(int testNumber, InputReader sc, PrintWriter out) throws IOException { while(sc.hasNext()) { int n=sc.nextInt(); int m=sc.nextInt(); int s=sc.nextInt(); int t=sc.nextInt(); int k=sc.nextInt(); int lim=sc.nextInt(); ArrayList<int[]>[] G=new ArrayList[n+1]; ArrayList<int[]>[] backG=new ArrayList[n+1]; for(int i=0;i<=n;i++) { G[i]=new ArrayList<int[]>(); backG[i]=new ArrayList<int[]>(); } for(int i=0;i<m;i++) { int u=sc.nextInt(); int v=sc.nextInt(); int w=sc.nextInt(); G[u].add(new int[] {v,w}); backG[v].add(new int[] {u,w}); } PriorityQueue<long[]> que=new PriorityQueue<long[]>(new Comparator<long[]>() { @Override public int compare(long[] o1, long[] o2) { if(o1[1]>o2[1]) return 1; if(o1[1]<o2[1]) return -1; return 0; } }); que.offer(new long[] {t,0}); long[] f=new long[n+1]; Arrays.fill(f, Integer.MAX_VALUE); f[t]=0; boolean[] vis=new boolean[n+1]; while(!que.isEmpty()) { long[] temp=que.poll(); int u=(int) temp[0]; long cost=temp[1]; if(vis[u]) continue; vis[u]=true; for(int[] edge:backG[u]) { if(f[edge[0]]>cost+edge[1]) { f[edge[0]]=cost+edge[1]; que.offer(new long[] {edge[0],f[edge[0]]}); } } } out.println(aStar(s,t,G,f,k,n,lim)?"yareyaredawa":"Whitesnake!"); } } } static class InputReader{ StreamTokenizer tokenizer; public InputReader(InputStream stream){ tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream))); tokenizer.ordinaryChars(33,126); tokenizer.wordChars(33,126); } public String next() throws IOException { tokenizer.nextToken(); return tokenizer.sval; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean hasNext() throws IOException { int res=tokenizer.nextToken(); tokenizer.pushBack(); return res!=tokenizer.TT_EOF; } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } public BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } } }改写成c++代码
最新发布
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值