简体   繁体   English

如何有效使用ListIterator?

[英]How to use ListIterator effectively?

I have a method like the following -: 我有如下方法-

public void apply(ArrayList<String> tokenStream);

in which I accept a list of string tokens like the following - tokenStream = ("I am good" , "I am bad") , and I want to be able to convert these tokens in the tokenStream list into individual tokens and add them back to the same list in the manner that after the method execution is over I should have - tokenStream = ("I" , "am" ,"good" , "I" , "am" , "bad"), How can I use ListIterator to solve this problem? 在其中,我接受如下所示的字符串标记列表-tokenStream =(“我很好”,“我不好”),并且我希望能够将tokenStream列表中的这些标记转换为单个标记并将其重新添加以相同的方式,方法执行结束后,我应该有-tokenStream =(“ I”,“ am”,“ good”,“ I”,“ am”,“ bad”),我该如何使用ListIterator解决了这个问题? Basically I want to be able to split the individual strings in the tokenStream and add them back to the same stream object using ListIterators set and remove methods, how do I do it? 基本上,我希望能够使用ListIterators set和remove方法在tokenStream中拆分单个字符串并将其添加回同一流对象,该怎么办?

This is specifically what I am trying to do 这就是我要尝试做的

public void apply(tokenstream stream){

        if (stream != null) {

            ArrayList<String> splittedTokens = new ArrayList<>();

            while (stream.hasNext()) {

                String token = stream.next();

                String[] splitTokens = token.split("\\s+");

                for (int i = 0; i < splitTokens.length; ++i)
                    splittedTokens.add(splitTokens[i]);
            }

            // set the stream to hold this new formed stream with tokens
            stream.set((String[]) splittedTokens.toArray());
        }
    }

I am trying to remove the whitespace from the tokens in the token stream object which internally uses an array list to store the tokens and this tokenstream class has a set of methods complementing those of the ListIterator class like set and remove, so how do I go about implementing that? 我正在尝试从令牌流对象中的令牌中删除空格,该对象内部使用数组列表来存储令牌,并且此tokenstream类具有一组与ListIterator类的方法互补的方法,例如set和remove,所以我该如何去关于实施?

对于此任务,最好的方法似乎是创建一个临时列表,迭代tokenStream拆分令牌并将其添加到临时列表中,然后从temp中清除tokenStream和addAll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM