我正在尝试使用for循环和listIterator方法迭代两个链表。
我正在尝试完成下面的方法,使用链表添加两个多项式
public Polynomial add(Polynomial p)
POSTCONDITION:此对象和p未更改返回一个多项式,它是p和这个多项式的和
这是我的方法
public Polynomial add( Polynomial p )
{
// use the copy constructor
Polynomial answer = new Polynomial( this );
// answer.termList.addAll(p.termList);
// use addAll()
//answer=this.termList.addAll(p.termList);
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr = answer.termList.listIterator();
//ListIterator<Term> itr2 = p.termList.listIterator();
for ( ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext(); )
{
Term term = itr.next();
for ( ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext(); )
{
Term term2 = itr2.next();
if ( term.exponent == term2.exponent )
{
answer.itr.coefficient = answer.itr.coefficient + p.itr2.coefficient;
}
}
/**while ( itr.hasNext() )
{
Term term = itr.next();
}**/
return answer;
}
}
这条线路for (ListIterator<Term> itr=answer.termList.listIterator();itr.hasNext();)
和for(ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();)
有错误。
我一直收到一个错误,说itr和itr2无法解决或不是字段。
这里是定义多项式的代码的一部分,有很多方法我没有包括在内,因为它们太长了。
public class Polynomial implements Cloneable
{
// this is a nested static class, it defines a type
// all the instance varaibles can be access directly inside Polynomial
// class even though they have
// private modifier
private static class Term
{
private int exponent;
private double coefficient;
public Term( int exp, double coeff )
{
coefficient = coeff;
exponent = exp;
}
}
// instance variables of Polynomial
// first is the term of the Polynomial with the highest degree
private LinkedList<Term> termList;
/**
* Postcondition: Creates a polynomial which is 0.
**/
public Polynomial()
{
termList = new LinkedList<Term>();
termList.add( new Term( 0, 0 ) );
}
/**
* Postcondition: Creates a polynomial which has a single term
* a0*x^0
*
* @param a0 The value to be set as the coefficient of the
* constant (x^0) t erm.
**/
public Polynomial( double a0 )
{
termList = new LinkedList<Term>();
termList.add( new Term( 0, a0 ) );
}
}
这是故障线路
answer.itr.coefficient= answer.itr.coefficient + p.itr2.coefficient;
itr和itr2是迭代器对象,而不是Polynomial
的成员,我想你的意思是
term.coefficient += term2.coefficient;
更新
为了完整起见,这里是我的add
方法的完整版本
public Polynomial add(Polynomial p) {
Polynomial answer = new Polynomial(this);
for (ListIterator<Term> itr = answer.termList.listIterator(); itr.hasNext();) {
Term term = itr.next();
for (ListIterator<Term> itr2 = p.termList.listIterator(); itr2.hasNext();) {
Term term2 = itr2.next();
if (term.exponent == term2.exponent) {
term.coefficient += term2.coefficient;
} else {
answer.termList.add(new Term(term2.exponent, term2.coefficient));
}
}
}
return answer;
}
循环不必嵌套。
此解决方案将按指数的降序对术语进行排序:
public class Polynomial {
private List<Term> termList = new LinkedList<Term>();
private static class Term {
private int exponent;
private double coefficient;
private Term(int exponent, double coefficient) {
this.exponent = exponent;
this.coefficient = coefficient;
}
}
public Polynomial() {
}
public Polynomial(int exponent, double coeff) {
if (coeff != 0) {
termList.add(new Term(exponent, coeff));
}
}
public Polynomial add(Polynomial other) {
Polynomial answer = new Polynomial();
Iterator<Term> itr = termList.iterator();
Iterator<Term> itr2 = other.termList.iterator();
Term t = itr.hasNext() ? itr.next() : null;
Term t2 = itr2.hasNext() ? itr2.next() : null;
while (true) {
if (t == null) {
if (t2 == null) {
break;
}
answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
} else if (t2 == null) {
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
} else if (t2.exponent > t.exponent) {
answer.termList.add(new Term(t2.exponent, t2.coefficient));
t2 = itr2.hasNext() ? itr2.next() : null;
} else if (t2.exponent < t.exponent) {
answer.termList.add(new Term(t.exponent, t.coefficient));
t = itr.hasNext() ? itr.next() : null;
} else {
answer.termList.add(
new Term(t.exponent, t.coefficient + t2.coefficient));
t = itr.hasNext() ? itr.next() : null;
t2 = itr2.hasNext() ? itr2.next() : null;
}
}
return answer;
}
}