[英]How to detect polynomials in Java
假设我有一个多项式-x ^ 2 + 2x - 1 = 0.它是从文件中读取的。
我有分析多项式的每个字符的代码。
我想创建一个额外的步骤来压缩多项式(因此白色空间被消除)所以我可以检查字符串是否实际上是一个多项式,我可以通过检查多项式的最后2个索引来轻松完成标志和零这样:(= 0)
问题是一些多项式长度有不同的长度,这让我想到使用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);
}
}
如果要使用数组,您当然可以声明ArrayList<Character>
。 但是,无论如何,您可能希望使用StringBuilder
而不是列表来实现此目的。
st.replaceAll("\\s","")
删除字符串st中的所有空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.