简体   繁体   中英

How to detect polynomials in Java

Suppose I have a polynomial -x^2 + 2x - 1 = 0. It is read from a file.

I have the code that analyzes each character of the polynomial.

I want to create an extra step that compacts the polynomial(so the white spaces gets eliminated) so I can check if the string is in fact a polynomial which I can easily do by just checking the last 2 index of the polynomial which is the equal sign and the zero like this: (=0)

Problem is some polynomial length have different lengths which gave me the thought to use an ArrayList. Problem is I cannot declare my ArrayList to be of type Character to store each character in the sequential index of an ArrayList.

public void createEquationNoWhiteSpaces(){
    // it cannot be done because there is no ArrayList of characters
    textArrayList = new ArrayList<String>();
    for(int i = 0; i < text.length(); i++){
        // Store the characters of the polynomial in an ArrayList
        // because each polynomial has different length
        if(text.charAt(i) != ' ')
            textArrayList = text.charAt(i);
    }
}

If you want to use an array, you can certainly declare an ArrayList<Character> . However, you might want to use a StringBuilder instead of a list for this purpose anyway.

st.replaceAll("\\s","") 

删除字符串st中的所有空格

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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