我正在尝试使用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 ) );
}
}
答案 0 :(得分:0)
这是故障线路
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;
}
答案 1 :(得分:0)
循环不必嵌套。
此解决方案将使术语按指数的降序排列:
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;
}
}