CODE
ArrayList<String> tokens = new ArrayList<String>();
ArrayList<String> PRINT = new ArrayList<String>();
String tok = "";
for(String element : list) {
StringTokenizer st = new StringTokenizer(element);
if(!element.startsWith("PRINT")) {
while(st.hasMoreTokens()) {
tok = st.nextToken();
tokens.add(tok);
for(String key : expression.keySet())
if(tok.equals(key))
System.out.println(tokens.get(0) + " changed from " + expression.get(key) + " to " + Integer.parseInt(tokens.get(2)));
}
expression.put(tokens.get(0),Integer.parseInt(tokens.get(2)));
tokens.clear();
}
我在下面的行
中得到一个数组超出范围的例外System.out.println(tokens.get(0) + " changed from " + expression.get(key) + " to " + Integer.parseInt(tokens.get(2))
我知道tokens.get(0)=&#34; A&#34;,tokens.get(1)=&#34; =&#34;和tokens.get(2)=&#34; 7&#34;
答案 0 :(得分:0)
问题是,第一次向tokens
添加令牌时,只会有一个项目,但是for
循环需要3(在索引0,1和2处)。
答案 1 :(得分:0)
而不是代码
for(String element : list) {
StringTokenizer st = new StringTokenizer(element);
if(!element.startsWith("PRINT")) {
while(st.hasMoreTokens()) {
tok = st.nextToken();
tokens.add(tok);
使用以下代码
for(String element : list) {
for(int i=0;i<element.length();i++){
tokens.add(element.charAt(i)+"");
}