简体   繁体   English

如何在Java中检测多项式

[英]How to detect polynomials in Java

Suppose I have a polynomial -x^2 + 2x - 1 = 0. It is read from a file. 假设我有一个多项式-x ^ 2 + 2x - 1 = 0.它是从文件中读取的。

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) 我想创建一个额外的步骤来压缩多项式(因此白色空间被消除)所以我可以检查字符串是否实际上是一个多项式,我可以通过检查多项式的最后2个索引来轻松完成标志和零这样:(= 0)

Problem is some polynomial length have different lengths which gave me the thought to use an ArrayList. 问题是一些多项式长度有不同的长度,这让我想到使用ArrayList。 Problem is I cannot declare my ArrayList to be of type Character to store each character in the sequential index of an ArrayList. 问题是我不能将我的ArrayList声明为Character类型,以将每个字符存储在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> . 如果要使用数组,您当然可以声明ArrayList<Character> However, you might want to use a StringBuilder instead of a list for this purpose anyway. 但是,无论如何,您可能希望使用StringBuilder而不是列表来实现此目的。

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

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

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

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