Checking if an item in a String[] appears in a stream
<p>I'm currently writing a parser for a language that I'm creating, which needs to check if the current part of the stream fits one of the items in the passed array. A short version of the code is:</p>

<pre><code>public abstract class Parser {
    private StringReader reader; //This is a BufferedReader with rollback

    //A single string lookahead method
    public boolean lookahead(String toMatch, boolean rollback) throws ParseException {
        char c;
        //Mark the current position in the stream, so we can come back to it if needed
        MarkToken currentMark = reader.mark();

        //Iterate through the toMatch and check if each character matches
        for(int i = 0; i &lt; toMatch.length(); ++i) {
            c = reader.nextChar();

            if(toMatch.charAt(i) != c) {
                break;
            }
        }

        //Get the current image
        String got = reader.currentImage(currentMark);

        //If we don't have a match, rollback if necessary and return false
        if(!got.equals(toMatch)) {
            if(rollback) {
                reader.rollBack();
            }

            return false;
        }

        return true;
    }

    //The String[] lookahead method
    public int lookahead(String[] toMatch, boolean rollback) throws ParseException {
        if(toMatch.length == 1) {
            //If there is only one element in toMatch, send it to a cheaper function
            if(lookahead(toMatch[0]))
                return 0;
            else return 1;
        } else {
            int maxLength = toMatch[0].length();
            //We use this variable to keep track of how many valid choices are left
            int choicesLeft = toMatch.length;
            int i, j;
            char current;
            //Mark the current position in the stream, so we can come back to it if needed
            MarkToken mark = s().mark();

            //Get the length of the longest string in toMatch
            for(i = 1; i &lt; toMatch.length; ++i) {
                maxLength = Math.max(maxLength, toMatch[i].length());
            }

            //Go up to the length of the longest string
            for(i = 0; i &lt; maxLength; ++i) {
                //Get the next character from the stream
                current = reader.nextChar();

                //If we've reached the end of the stream:
                if(current == -1 || current == '\uffff') {
                    //Get back a character in the stream
                    reader.rollbackChar();

                    //And check to see if we have a match
                    return ArrayUtils.indexOf(toMatch, reader.currentImage(mark));
                }

                //Go through each item in toMatch
                for(j = 0; j &lt; toMatch.length; ++j) {
                    if(toMatch[j] != null) {
                        //Check to see if the character matches or not
                        if(toMatch[j].charAt(i) != current) {
                            //We null an item in toMatch if it doesn't apply any more
                            toMatch[j] = null;
                            --choicesLeft;
                        }
                    }
                }

                //If we only have one choice left, see if there is a match (will return -1 if not)
                if(choicesLeft == 1) {
                    return ArrayUtils.indexOf(toMatch, reader.currentImage(mark));
                }
            }

            //If there is no 
            if(rollback) {
                reader.rollBackTo(mark);
            }
        }

        return -1;
    }
}
</code></pre>

<p>This function would be called to check if the stream contained certain symbols (. .* $@ // " ' """ ''' etc.) and consume them greedily.</p>

<p>I would only be providing an array of maximum 10-15 items at once, so removing items from the array may not be the best optimisation.</p>

<p>Is there a more efficient way of doing this, certain methods or loops that I should be using?</p>
